View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Use Value of Dynamically Created Variable

Thank you so much Garry. The code snippet I provided was an example,
as the actual code is too much to post here, I believe. I really
like your idea, though, and am going to incorporate it into the code.
What I meant by dynamic is that the variable name is dynamic. I
still would like to know, if I create the name of a variable
dynamically as a string, how do I then retrieve the actual value of
the variable? Ages ago, we used to use something like Temp =
&VariableName to store the value of the variable "VariableName", but
I don't believe that was Visual Basic.


You're welcome! Thanks for the feedback...

<FWIW
Variables are declared components of code in VB[A], as in most
languages. You can ref them 'as declared' and assign values to them
that can be retrieved later as your intent indicates, but not the same
way as CallByName works (as joeu explains) because variables do not
have string names. To access a variable in this way it has to be
declared as a property of a class (as joeu suggests) because the
CallByName function only works for procedures that are defined as
public methods (of a class), and properties of a class that are defined
with public scope.

You can define your variables as UDTs also, but you must ref them
individually same as you must with your code sample. IMO, the most
efficient way is to use an array to access the values in a loop as I
exampled. If the values are fixed (ie: never change) then you can hard
code them and use something like this...

Dim vValue, n&
Const MyValues$ = "5,6,7,8"

For Each vValue In Split(MyValues, ",")
Debug.Print vValue 'string
Debug.Print CLng(vValue) 'long integer
Debug.Print CInt(vValue) 'integer
Next 'vValue

-OR-

vValue = Split((MyValues, ",")
For n = LBound(vValue) To UBound(vValue)
Debug.Print vValue(n) 'string
Debug.Print CLng(vValue(n)) 'long integer
Debug.Print CInt(vValue(n)) 'integer
Next '

...where vValue is treated as a Variant because no 'Type' was declared.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion



---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com