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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,694
Office Version
  1. 365
Platform
  1. Windows
"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
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,820
Office Version
  1. 2010
Platform
  1. Windows
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
 

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,182
Office Version
  1. 2013
Platform
  1. Windows
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
 

tom88Excel

New Member
Joined
Sep 29, 2014
Messages
30

ADVERTISEMENT

"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 !!!
 

tom88Excel

New Member
Joined
Sep 29, 2014
Messages
30
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!
 

tom88Excel

New Member
Joined
Sep 29, 2014
Messages
30
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,130,312
Messages
5,641,468
Members
417,210
Latest member
rins

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
Top