Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
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.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 79
Default Loop with variable name?

How about PN&i

edvwvw

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.


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...excel/200904/1

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default Loop with variable name?

You can not create varaible references on the fly. You are better off with an
array.

Dim lng As Long
Dim PN(12) As String

PN(0) = "A"
PN(1) = "B"
PN(2) = "C"
PN(3) = "D"

For lng = 0 To 11
MsgBox PN(lng)
Next lng

--
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.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Loop with variable name?

Use an array.

Dim arrPI
Dim intTemp

arrPI =("a","b","c","d")
For intTemp = 0 to Ubound(arrPI)
Msgbox arrPI(intTemp)
Next


If you want to assign variables at run time declare

Dim arrPI(10) as Variant
arrPI(0) = "Something0"
arrPI(1) = "Something1"
arrPI(2) = "Something2"
arrPI(3) = "Something2"
..
upto (You can store a max of 11 variables in this array)
arrPI(10) = "Something10"






If this post helps click Yes
---------------
Jacob Skaria


"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.

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Loop with variable name?

Use an array:

Option Base 1
Sub Mike()
Dim PN(12) As Integer
For i = 1 To 12
PN(i) = i
Next
End Sub

--
Gary''s Student - gsnu200849


"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.



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Loop with variable name?

Hopefully it isn't too late for you to make this change:

Instead of using variables named PN1, PN2 ... PN12, use an array, which you
could call PN. Set it up this way:

Dim PN(1 to 12) As type
where type refers to the type of values to go into it, as Integer (whole
numbers), Long (great big whole numbers), single, double, etc.

Then instead of statements like PN1 = 4, you refer to them as PN(1) = 4,
PN(9) = 6, etc.

So in your loop, it all becomes very easy:

For i = 1 to 12
x = PN(i) + 5
Next i

Of course, in your example, x is always going to end up being the value in
PN(12) + 5, but I presume it's a trivial example.

Better even than using For i = 1 to 12, do this:

For i = LBound(PN) To UBound(PN)
x = PN(i) + 5
Next i

LBound and UBound refer to the lower and upper boundary of the array
referenced. So if you ever change the number of elements in the array, your
loop automatically adjusts to the new size.

Hope this helps some.

"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.

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Loop with variable name?

A small correction

Use an array.

Dim arrPI
Dim intTemp

arrPI = Array("a","b","c","d")
For intTemp = 0 to Ubound(arrPI)
Msgbox arrPI(intTemp)
Next


If you want to assign variables at run time declare

Dim arrPI(10) as Variant
arrPI(0) = "Something0"
arrPI(1) = "Something1"
arrPI(2) = "Something2"
arrPI(3) = "Something2"
..
upto (You can store a max of 11 variables in this array)
arrPI(10) = "Something10"


If this post helps click Yes
---------------
Jacob Skaria

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
Insert Variable Number of Rows; With Loop ryguy7272 Excel Worksheet Functions 2 December 27th 06 08:25 PM
Loop Macro a variable number of times thesaxonuk Excel Discussion (Misc queries) 11 October 31st 06 06:05 PM
variable height variable width stacked bar charts ambthiru Charts and Charting in Excel 3 January 18th 06 11:41 PM
Sum cells based on a row variable and seperate column variable CheeseHeadTransplant Excel Worksheet Functions 10 September 23rd 05 06:59 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM


All times are GMT +1. The time now is 05:51 AM.

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

About Us

"It's about Microsoft Excel"