Can you please help me with a simple question?.

tom88Excel

New Member
Joined
Sep 29, 2014
Messages
30
How do I make the output below as "Apple", "Orange", "Pear", "Banana" instead of "Name1", "Name2", "Name3" & "Name4" ?

Also, is there a way to declare a bunch of variables using "to" ? For example, something like Dim ("Name" & 1 to "Name" & 20) as String?


VBA Code:
Dim Name1 As String, Name2 As String, Name3 As String, Name4 As String

Name1 = "Apple"
Name2 = "Orange"
Name3 = "Pear"
Name4 = "Banana"

    Debug.Print Name1'This one works, but the below don't, why? How do I make this work?

For i = 1 To 6
    Debug.Print "Name" & i
Next i
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
"Name" & i is a text string, not a variable name.

What you are trying is probably easiest done with a simple array.
VBA Code:
Dim Names(1 To 4) As String, i As Long
Names(1) = "Apple"
Names(2) = "Orange"
Names(3) = "Pear"
Names(4) = "Banana"
For i = 1 To 4
    Debug.Print Names(i)
Next i
 
Upvote 0
VBA doesn't support variable names being created from other variables i.e there is no equivalence of the indirect function, what are trying to do? becuase there are other ways of coding that will probably meet your needs
 
Upvote 0
Yes, array is the way to go.
Your circumstances will determine how yo fill it. Eg hard coded or from a range of cells.
All sorts of ways you may deal with it.

VBA Code:
Dim Names As Variant
Names = Split("Tom, ****, Harry, Fred, Sam, Herbert, Arthur", ",")
    
For i = 0 To UBound(Names)
    Debug.Print Trim(Names(i))
Next i
 
Upvote 0
"Name" & i is a text string, not a variable name.

What you are trying is probably easiest done with a simple array.
VBA Code:
Dim Names(1 To 4) As String, i As Long
Names(1) = "Apple"
Names(2) = "Orange"
Names(3) = "Pear"
Names(4) = "Banana"
For i = 1 To 4
    Debug.Print Names(i)
Next i

This is exactly what I need. Thank you so much jasonb75 !!!
 
Upvote 0
Yes, array is the way to go.
Your circumstances will determine how yo fill it. Eg hard coded or from a range of cells.
All sorts of ways you may deal with it.

VBA Code:
Dim Names As Variant
Names = Split("Tom, ****, Harry, Fred, Sam, Herbert, Arthur", ",")
   
For i = 0 To UBound(Names)
    Debug.Print Trim(Names(i))
Next i

This is great. Thank you Snakehips. I like the split. this makes the code more simple. Thank you!
 
Upvote 0
VBA doesn't support variable names being created from other variables i.e there is no equivalence of the indirect function, what are trying to do? becuase there are other ways of coding that will probably meet your needs

I have a range of text I need to loop I'm still a bit confused about my original code... Thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top