refer variable name by value from string

zico8

Board Regular
Joined
Jul 13, 2015
Messages
210
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:

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,939
To refer to named ranges you just need to use the range object:

Code:
[COLOR=#333333]msgbox = Range("temp_") & selection.row[/COLOR]
 

zico8

Board Regular
Joined
Jul 13, 2015
Messages
210
Thanks gallen but it does not work,

It would be if I had defined these names inside worksheet but I have these as variables.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
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
 

zico8

Board Regular
Joined
Jul 13, 2015
Messages
210
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,206
Office Version
365
Platform
Windows
AFAIK there is no way of getting a variables contents by concatenating strings.
 

zico8

Board Regular
Joined
Jul 13, 2015
Messages
210
Are you sure?

In a worksheet I would probably do it with INDIRECT function.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,338
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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. ;)
 

zico8

Board Regular
Joined
Jul 13, 2015
Messages
210
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,338
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
That looks ideal for an array as Fluff showed you.
 

Forum statistics

Threads
1,081,992
Messages
5,362,599
Members
400,683
Latest member
LogChief

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top