salibes
asked on
declare and initialize many variables
I want to declare and initialize 9 variables.... named var1, var2 ... var9 , initialized to the values of 1 to 9 (or anything else)...
how would i do it with a do while loop
The loop could for example be an iterative for num n <9 , n++ in the loop
Thanks
how would i do it with a do while loop
The loop could for example be an iterative for num n <9 , n++ in the loop
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops, colVars.add "var" & i
Use an array but declare it for many elements then do a redim preserve to get rid of the extras...
ReDim var(1 to 100) as string
'Loop code
Do while x <= 50
'Do whatever
x = x + 1
Loop
ReDim Preserve var(1 to x) as string
ReDim var(1 to 100) as string
'Loop code
Do while x <= 50
'Do whatever
x = x + 1
Loop
ReDim Preserve var(1 to x) as string
ASKER
Thank you,
If I have a double, and date variable, do I do the same thing..?
If I have a double, and date variable, do I do the same thing..?
That should work fine. Create separate arrays for each type of variable.
sure
you can use a collection and store whatever type you want:
Dim col As New Collection
col.Add 1.01
col.Add Now
col.Add "test"
For i = 1 To col.Count
MsgBox col(i)
Next
Dim col As New Collection
col.Add 1.01
col.Add Now
col.Add "test"
For i = 1 To col.Count
MsgBox col(i)
Next
ASKER
for collection.. col.add "test" .. does that initilize the value col(3) to "test" or create an uninitialized col("test") variable?
initializes col(3) to "test"
you can also specify a key when adding to collection:
ie: col.add("hello world", "test")
then, col("test") should give you "hello world"
you can also specify a key when adding to collection:
ie: col.add("hello world", "test")
then, col("test") should give you "hello world"
ASKER
thank you, let me try that and see if it works
ASKER
It says 'expected :='
---
Dim values As Collection
values.Add("val", "SE")
---
Dim values As Collection
values.Add("val", "SE")
remove parentheses:
values.Add "val", "SE"
or
values.Add Item:="val", Key:="SE"
values.Add "val", "SE"
or
values.Add Item:="val", Key:="SE"
ASKER
almost there -
i got a "complie error: named argument already specified"
--- for this code
values.Add Item:=Range("A" & cellNum & "").Value, Item:="SE" & varNum
i got a "complie error: named argument already specified"
--- for this code
values.Add Item:=Range("A" & cellNum & "").Value, Item:="SE" & varNum
ASKER
i changed the second item to key..
ok, so what's the problem now?
ASKER
I now get
"runtime error '91' Object variable or with block variable not set"
---code
Dim cellNum, varNum As Integer
cellNum = 2
varNum = 1
values.Add Item:=Range("A" & cellNum).Value, key:="SE" & varNum
"runtime error '91' Object variable or with block variable not set"
---code
Dim cellNum, varNum As Integer
cellNum = 2
varNum = 1
values.Add Item:=Range("A" & cellNum).Value, key:="SE" & varNum
make sure you do:
set values = new Collection
set values = new Collection
ASKER
Thank you, youve been more than helpful, I am going to award you the points.. if you wouldnt mind looking at this last thing..(i hope) it now gives me ...
"run time error '5': Invalid procedure call or argument"
--code
Range("C" & i).Value = values.Item("resourceCode" & varNum)
error from the values.item
"run time error '5': Invalid procedure call or argument"
--code
Range("C" & i).Value = values.Item("resourceCode"
error from the values.item
what does
msgbox values.item("resourceCode" & varNum) show?
msgbox values.item("resourceCode"
ASKER
doesnt display gives the same invalid procedure call error..., if i have var = "resourceCode" & varNum, it displays correctly.... is there a reference i should add?
can i see the whole segment of code, including where you are adding items to the collection?
ASKER
Very helpful and responsive
ASKER