refer variable name by value from string

zico8

Board Regular
Joined
Jul 13, 2015
Messages
227
Hi,

I have some defined variables where have added some values:

temp_1 = "red"
temp_2 = "black"
temp_3 = "blue"
etc.

how can I refer to the correct one by text string?

msgbox = "temp_" & selection.row
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
To refer to named ranges you just need to use the range object:

Code:
[COLOR=#333333]msgbox = Range("temp_") & selection.row[/COLOR]
 
Upvote 0
Thanks gallen but it does not work,

It would be if I had defined these names inside worksheet but I have these as variables.
 
Upvote 0
How about
Code:
Sub demo()
   Dim temp(1 To 3) As String
   Dim i As Long
   
   For i = 1 To 3
      temp(i) = Choose(i, "Red", "Black", "Blue")
   Next i
   
   MsgBox temp(Selection.Row)
End Sub
 
Upvote 0
Yes,

It works for sample I have given here.

But my issue is a little different in fact. I am stricte interested how can I add any of string to variable name I am looking for.
 
Upvote 0
AFAIK there is no way of getting a variables contents by concatenating strings.
 
Upvote 0
You could store the variables in a collection or dictionary, or use a class to hold the variables. There are generally better ways of approaching it though - whenever I've seen people ask this question in the past, they have been overcomplicating things. ;)
 
Upvote 0
Hi RoryA,

I am looking for that way because I have several variables storing values and need to get these into table where its headers are named the same as part of variable's names.

For example my tyble headers are named "1", "2", "3" and this is why I try to get the correct variable value into correct table column.
defined variables:
Code:
[COLOR=#333333]temp_1 = "red"[/COLOR]
[COLOR=#333333]temp_2 = "black"[/COLOR]
[COLOR=#333333]temp_3 = "blue"[/COLOR]
Code:
[COLOR=#26282A][FONT=&quot]for my_table_column = 1 to my_table.listcolumns.count
[/FONT][/COLOR]my_table.range.cells(i, my_table_column) = "temp_" & my_table.headerrowrange(my_table_column).value
next my_table_column
 
Upvote 0
That looks ideal for an array as Fluff showed you.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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