' I have 20 instances of this argument:
If Category1 = "Tea" Then
Teas = Trange1
End If
' Through
If Category20 = "Tea" Then
Teas = Trange20
End If
' Where each Category (1-20) is a variable resulting from a Vlookup based on the value in
' each Trange (1-20), and each Trange is a variable derived from the changing contents
' of 20 ranges. This code works perfectly to place the type of tea, say, “English Breakfast”, in the
' named range Teas.
' I have several similar arguments which are also repeated 20 times, and the code has become rather
' long. I tried to shorten the code by looping, but apparently I need to learn a little something about
' working with variables as my best efforts have failed. What is missing from the code below?:
Dim i As Integer
Dim Va As String
Dim Vb As String
For i = 1 To 20
Va = "Category" & i
Vb = "Trange" & i
If Va = "Tea" Then
Teas = Vb
End If
Next
‘MsgBox Category1 results in "Teas", but, MsgBox Va results in “Category1” (through Category20),
' rather than its stored result "Teas".
' Likewise, MsgBox Trange1 results in "English Breakfast" while MsgBox Vb results in “Trange1” rather
' than its stored result “English Breakfast”. How do I get the variable to reference the result stored in
' the other variable, rather than the name of the other variable?
If Category1 = "Tea" Then
Teas = Trange1
End If
' Through
If Category20 = "Tea" Then
Teas = Trange20
End If
' Where each Category (1-20) is a variable resulting from a Vlookup based on the value in
' each Trange (1-20), and each Trange is a variable derived from the changing contents
' of 20 ranges. This code works perfectly to place the type of tea, say, “English Breakfast”, in the
' named range Teas.
' I have several similar arguments which are also repeated 20 times, and the code has become rather
' long. I tried to shorten the code by looping, but apparently I need to learn a little something about
' working with variables as my best efforts have failed. What is missing from the code below?:
Dim i As Integer
Dim Va As String
Dim Vb As String
For i = 1 To 20
Va = "Category" & i
Vb = "Trange" & i
If Va = "Tea" Then
Teas = Vb
End If
Next
‘MsgBox Category1 results in "Teas", but, MsgBox Va results in “Category1” (through Category20),
' rather than its stored result "Teas".
' Likewise, MsgBox Trange1 results in "English Breakfast" while MsgBox Vb results in “Trange1” rather
' than its stored result “English Breakfast”. How do I get the variable to reference the result stored in
' the other variable, rather than the name of the other variable?
Last edited: