Combining variable names

B

Bernie Gaile

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
 
T

Tim Barlow

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
 
G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top