Link to home
Start Free TrialLog in
Avatar of salibes
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
Avatar of salibes
salibes

ASKER

is there a way to do this with a vector? can't use array because i'm not sure of the size until loop ends, and dont want to then run another loop because that defeats my purpose of trying to increase efficiency.
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of salibes

ASKER

Thank you,

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
Avatar of salibes

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"
Avatar of salibes

ASKER

thank you,  let me try that and see if it works
Avatar of salibes

ASKER

It says 'expected :='

---

Dim values As Collection
values.Add("val", "SE")
 
remove parentheses:

values.Add "val", "SE"  
or
values.Add Item:="val", Key:="SE"
Avatar of salibes

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
Avatar of salibes

ASKER

i changed the second item to key..
ok, so what's the problem now?
Avatar of salibes

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
make sure you do:

set values = new Collection
Avatar of salibes

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
what does
msgbox values.item("resourceCode" & varNum) show?
Avatar of salibes

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?
Avatar of salibes

ASKER

Very helpful and responsive