Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default 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)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default 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)


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default 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)

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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)

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop with variable name? Mike Excel Discussion (Misc queries) 6 April 25th 09 05:12 AM
LAST value of loop to be a variable Berj Excel Programming 2 December 5th 07 10:19 AM
loop for-next with variable end Valeria Excel Programming 6 November 30th 07 04:14 PM
For Each ... Next loop - need to reference the loop variable [email protected] Excel Programming 4 July 13th 06 06:12 PM
Loop with variable Knut Excel Programming 2 November 19th 05 02:48 PM


All times are GMT +1. The time now is 12:14 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"