ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Combining variable names (https://www.excelbanter.com/excel-programming/286352-combining-variable-names.html)

Bernie Gaile

Combining variable names
 
Hey guys,
I have 3 variables: var1, var2, var3
var1 = "va"
var2 = "r3"
I want to check 'if var3 = 1' by using var1 and var2

something like 'If var1 & var2 = 1 then'
but I've tried the above and it doesn't work

Is there a way to combine variables names like this?

Thank you,
Bernie

Tim Barlow

Combining variable names
 
Bernie,

I don't think you can do it with normal variables, but you could do it with
objects - e.g. using a Collection you could have something like:

Dim var1 As String
Dim var2 As String
Dim var3 As String
Dim x As New Collection

var1 = "var1"
var2 = "var2"
var3 = "var3"

x.Add key:=var1, Item:="va"
x.Add key:=var2, Item:="r3"
x.Add key:=var3, Item:=1

If x.Item(x.Item(var1) & x.Item(var2)) = 1 Then
MsgBox "Answer is 1"
End If

Only problem with using a 'keys & values' is that you can only add each key
once - if you want to replace it wirh a different values then you would have
to delete the item first.

HTH

Tim


"Bernie Gaile" wrote in message
...
Hey guys,
I have 3 variables: var1, var2, var3
var1 = "va"
var2 = "r3"
I want to check 'if var3 = 1' by using var1 and var2

something like 'If var1 & var2 = 1 then'
but I've tried the above and it doesn't work

Is there a way to combine variables names like this?

Thank you,
Bernie




No Name

Combining variable names
 
Thanks Tim.
Now, what if I want to combine text and a variable within
that collection.

something like:
newvar = 2
If x.Item(x.Item(var1) & x.Item("var" & newvar)) = 1

Am I able to something like that?

-----Original Message-----
Bernie,

I don't think you can do it with normal variables, but

you could do it with
objects - e.g. using a Collection you could have

something like:

Dim var1 As String
Dim var2 As String
Dim var3 As String
Dim x As New Collection

var1 = "var1"
var2 = "var2"
var3 = "var3"

x.Add key:=var1, Item:="va"
x.Add key:=var2, Item:="r3"
x.Add key:=var3, Item:=1

If x.Item(x.Item(var1) & x.Item(var2)) = 1 Then
MsgBox "Answer is 1"
End If

Only problem with using a 'keys & values' is that you

can only add each key
once - if you want to replace it wirh a different values

then you would have
to delete the item first.

HTH

Tim


"Bernie Gaile"

wrote in message
...
Hey guys,
I have 3 variables: var1, var2, var3
var1 = "va"
var2 = "r3"
I want to check 'if var3 = 1' by using var1 and var2

something like 'If var1 & var2 = 1 then'
but I've tried the above and it doesn't work

Is there a way to combine variables names like this?

Thank you,
Bernie



.



All times are GMT +1. The time now is 12:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com