Loop with Variable name?

M

Mike

Hi. I have 12 variables that are names PN1, PN2, .... PN12

I would like to have a "For - Next" loop that would execute for i = 1 to 12
where "i" would change in the variable name. I cannot figure out how to
refer to the variable:

For i = 1 to 12

x = PNi + 5

Next i

Any help would be appreciated ...

Thanks,
Mike.


(Note I posted this previously in error to the "General" section)
 
P

Per Jessen

Hi Mike

You could use an array variable.

Sub Mike()
Dim PN(1 To 12) As Long

For i = 1 To 12
x = PN(i) + 5
Next
End Sub

Hopes this helps.
 
J

Jack Dahlgren

I don't think that this sort of dynamic creation of variable name is possible
in VBA. Perhaps you could use an array like this?

Sub foo()
Dim PN(12) As String
Dim i As Integer
For i = 0 To 11
PN(i) = i + 5
Next i
For i = 0 To 11
MsgBox PN(i)
Next i
End Sub

-Jack Dahlgren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top