Indirectly referring to a variable

  • Follow


Hello,

This is probably easy ... or impossible, but I cannot figure out how to do 
this.  I want to assembly a variable name in code, and then get that 
variable.  Separate parts of my macro tell me my prefix is "Snow" and my 
suffix is "Shoe".  How to I get the value of "SnowShoe"?  I have a lot of 
these variables.  The following does not work:

VariableIWant="Snow" & "Shoe"
NumberINeed=5+VariableIWant

I ended up with a bunch of If statements.  Effective but not too elegant.  
Any help would be appreciated.  Thank you in advance.
0
Reply Utf 5/28/2010 3:32:01 PM

I am not clear on what you are after, but if you place a variable inside 
quote marks, it becomes a string and the value is what you see.  e.g. If the 
variable Snow = 25, when you place it inside quotes then "Snow" = "Snow". 
So, if you want the values of Snow and Shoe to be added it would be 
VariableWant = Snow + Shoe to get VariableWant to equal the sum of Snow + 
Shoe.  Then you could add NumberNeed = 5 + VariableWant and get a numeric 
value.  But if you use quotes around the variables, the all you get is 
concatenated strings.


"ZipCurs" <ZipCurs@discussions.microsoft.com> wrote in message 
news:05FC1CB2-AF15-4E08-9FAD-7D549C740C46@microsoft.com...
> Hello,
>
> This is probably easy ... or impossible, but I cannot figure out how to do
> this.  I want to assembly a variable name in code, and then get that
> variable.  Separate parts of my macro tell me my prefix is "Snow" and my
> suffix is "Shoe".  How to I get the value of "SnowShoe"?  I have a lot of
> these variables.  The following does not work:
>
> VariableIWant="Snow" & "Shoe"
> NumberINeed=5+VariableIWant
>
> I ended up with a bunch of If statements.  Effective but not too elegant.
> Any help would be appreciated.  Thank you in advance. 


0
Reply JLGWhiz 5/28/2010 4:44:40 PM


Where/how is the variable "SnowShoe" defined ?

If you really want to map data this way then you might use (eg) a
dictionary object

'**********************
dim d as object
set d=3Dcreateobject("scripting.dictionary")
d.add "SnowShoe", 20
d.add "RainShoe", 10
Msgbox d.Item("Snow" & "Shoe")  '20
'**********************



Tim


On May 28, 8:32=A0am, ZipCurs <ZipC...@discussions.microsoft.com> wrote:
> Hello,
>
> This is probably easy ... or impossible, but I cannot figure out how to d=
o
> this. =A0I want to assembly a variable name in code, and then get that
> variable. =A0Separate parts of my macro tell me my prefix is "Snow" and m=
y
> suffix is "Shoe". =A0How to I get the value of "SnowShoe"? =A0I have a lo=
t of
> these variables. =A0The following does not work:
>
> VariableIWant=3D"Snow" & "Shoe"
> NumberINeed=3D5+VariableIWant
>
> I ended up with a bunch of If statements. =A0Effective but not too elegan=
t. =A0
> Any help would be appreciated. =A0Thank you in advance.

0
Reply Tim 5/28/2010 5:46:25 PM

Hello Tim,

I am not sure it matters but lets say that ShowShoe is Double.  I get to a 
situation where I figure out what variable I want, and then need to get it.  
So I reach a point where I know I want SnowShoe, but I need a way of getting 
it.  I think something like what you gave me would work fine.  I will try it 
later.

"Tim Williams" wrote:

> Where/how is the variable "SnowShoe" defined ?
> 
> If you really want to map data this way then you might use (eg) a
> dictionary object
> 
> '**********************
> dim d as object
> set d=createobject("scripting.dictionary")
> d.add "SnowShoe", 20
> d.add "RainShoe", 10
> Msgbox d.Item("Snow" & "Shoe")  '20
> '**********************
> 
> 
> 
> Tim
> 
> 
> On May 28, 8:32 am, ZipCurs <ZipC...@discussions.microsoft.com> wrote:
> > Hello,
> >
> > This is probably easy ... or impossible, but I cannot figure out how to do
> > this.  I want to assembly a variable name in code, and then get that
> > variable.  Separate parts of my macro tell me my prefix is "Snow" and my
> > suffix is "Shoe".  How to I get the value of "SnowShoe"?  I have a lot of
> > these variables.  The following does not work:
> >
> > VariableIWant="Snow" & "Shoe"
> > NumberINeed=5+VariableIWant
> >
> > I ended up with a bunch of If statements.  Effective but not too elegant.  
> > Any help would be appreciated.  Thank you in advance.
> 
> .
> 
0
Reply Utf 5/28/2010 7:01:02 PM

Thank you for responding.  I think that Tim Williams' response is closer to 
the mark.  Basically, I have a bunch of variables, and am looking for an easy 
way to get what I want.  I want to use SnowShoe this time.  Next time I might 
want HikingBoot.

"JLGWhiz" wrote:

> I am not clear on what you are after, but if you place a variable inside 
> quote marks, it becomes a string and the value is what you see.  e.g. If the 
> variable Snow = 25, when you place it inside quotes then "Snow" = "Snow". 
> So, if you want the values of Snow and Shoe to be added it would be 
> VariableWant = Snow + Shoe to get VariableWant to equal the sum of Snow + 
> Shoe.  Then you could add NumberNeed = 5 + VariableWant and get a numeric 
> value.  But if you use quotes around the variables, the all you get is 
> concatenated strings.
> 
> 
> "ZipCurs" <ZipCurs@discussions.microsoft.com> wrote in message 
> news:05FC1CB2-AF15-4E08-9FAD-7D549C740C46@microsoft.com...
> > Hello,
> >
> > This is probably easy ... or impossible, but I cannot figure out how to do
> > this.  I want to assembly a variable name in code, and then get that
> > variable.  Separate parts of my macro tell me my prefix is "Snow" and my
> > suffix is "Shoe".  How to I get the value of "SnowShoe"?  I have a lot of
> > these variables.  The following does not work:
> >
> > VariableIWant="Snow" & "Shoe"
> > NumberINeed=5+VariableIWant
> >
> > I ended up with a bunch of If statements.  Effective but not too elegant.
> > Any help would be appreciated.  Thank you in advance. 
> 
> 
> .
> 
0
Reply Utf 5/28/2010 7:03:01 PM

You can't construct varibale names like this in VBA.

Using a dictionary might be a workaround, so might an array approach.
0
Reply norie 5/28/2010 8:41:26 PM

Hello Tim Williams,

Your recommendation works great.  The only addition is to use 

d.item("SnowShoe")= NewValue

to make "SnowShoe" behave like a variable and be able to call it by 
assembling the name.  Thanks.

"Tim Williams" wrote:

> Where/how is the variable "SnowShoe" defined ?
> 
> If you really want to map data this way then you might use (eg) a
> dictionary object
> 
> '**********************
> dim d as object
> set d=createobject("scripting.dictionary")
> d.add "SnowShoe", 20
> d.add "RainShoe", 10
> Msgbox d.Item("Snow" & "Shoe")  '20
> '**********************
> 
> 
> 
> Tim
> 
> 
> On May 28, 8:32 am, ZipCurs <ZipC...@discussions.microsoft.com> wrote:
> > Hello,
> >
> > This is probably easy ... or impossible, but I cannot figure out how to do
> > this.  I want to assembly a variable name in code, and then get that
> > variable.  Separate parts of my macro tell me my prefix is "Snow" and my
> > suffix is "Shoe".  How to I get the value of "SnowShoe"?  I have a lot of
> > these variables.  The following does not work:
> >
> > VariableIWant="Snow" & "Shoe"
> > NumberINeed=5+VariableIWant
> >
> > I ended up with a bunch of If statements.  Effective but not too elegant.  
> > Any help would be appreciated.  Thank you in advance.
> 
> .
> 
0
Reply Utf 5/29/2010 10:36:01 PM

6 Replies
182 Views

(page loaded in 0.464 seconds)


Reply: