ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop with Variable name? (https://www.excelbanter.com/excel-programming/427464-loop-variable-name.html)

Mike

Loop with Variable name?
 
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)

Per Jessen

Loop with Variable name?
 
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.

---
Per

"Mike" skrev i meddelelsen
...
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)



Jim Thomlinson

Loop with Variable name?
 
Please don't multi post. You have lots of replies in general...
--
HTH...

Jim Thomlinson


"Mike" wrote:

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)


Jack Dahlgren

Loop with Variable name?
 
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

"Mike" wrote:

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)



All times are GMT +1. The time now is 09:36 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com