Posted to microsoft.public.excel.programming
|
|
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.
On Sunday, December 1, 2013 3:03:54 PM UTC-5, GS wrote:
I fail to see anything 'dynamic' here since 'everything' is hard-coded!
Perhaps if your values were stored in a worksheet range (which could be
dynamic if desired) so they can be entered on-the-fly (ergo 'dynamic'!)
then your code won't need as much maintenance going forward...
Example 1: store values in a single row
Name: "Sheet1!MyValues"
RefersTo: =OFFSET($A$1,0,0,1,COUNTA($1:$1))
Content: [A1] 5, [B1] 6, [C1] 7, [D1] 8
Code example:
Dim vValues, n&
vValues = Range("MyValues")
For n = LBound(vValues, 2) To UBound(vValues, 2)
Debug.Print vValues(1, n)
Next 'n
Example 2: store values in a single column
Name: "Sheet1!MyValues"
RefersTo: =OFFSET($A$1,0,0,COUNTA($A:$A),1)
Content: [A1] 5, [A2] 6, [A3] 7, [A4] 8
Code example:
Dim vValues, n&
vValues = Range("MyValues")
For n = LBound(vValues) To UBound(vValues)
Debug.Print vValues(n, 1)
Next 'n
Example 3: store values in multiple rows/columns
Location: $A$1:$C$4
Name: "Sheet1!MyValues"
RefersTo: =OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1))
Dim vValues, n&, j&
vValues = Range("MyValues")
Code example:
(column values row by row)
For n = LBound(vValues) To UBound(vValues)
For j = LBound(vValues, 2) To UBound(vValues, 2)
Debug.Print vValues(n, j)
Next 'j
Next 'n
(row values column by column)
For n = LBound(vValues, 2) To UBound(vValues, 2)
For j = LBound(vValues) To UBound(vValues)
Debug.Print vValues(n, j)
Next 'j
Next 'n
I recommend using ranges outside the data area (above or to the left).
Optionally, you can store the data on a separate sheet (which could
also be hidden, if desired) but must ref that sheet in code...
vValues = Sheets("Sheet2").Range("MyValues")
--
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
|