ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Using a for loop (https://www.excelbanter.com/excel-discussion-misc-queries/118028-using-loop.html)

Jeff

Using a for loop
 
Hi,

I have a macro and it uses the for loop

For i=1 to 100
Call Macro2
Next i

Then in Macro2 there is another For loop.

Question = is it ok if the "For" loop in Macro2 uses the variable "i".
Should I declare the variable i in each macro, or declare a unique variable
in each macro.

Thanks for your help

Jim Thomlinson

Using a for loop
 
Depends on what you are up to as to the appropraite solution. The one thing I
will say is that within the loop you need to be sure not to change the value
of i. That will most likely mess up your looping. So if you want macro2 to
use i make sure that you pass it by value and not by reference. This will
ensure that i can not be permanently modified by macro2. so something like
this...

macro1
dim i as integer
For i=1 to 100
Call Macro2
Next i
end sub

macro2(byval i as integer)
i = i + 1 'will not change the value of i in macro 1
end sub
--
HTH...

Jim Thomlinson


"Jeff" wrote:

Hi,

I have a macro and it uses the for loop

For i=1 to 100
Call Macro2
Next i

Then in Macro2 there is another For loop.

Question = is it ok if the "For" loop in Macro2 uses the variable "i".
Should I declare the variable i in each macro, or declare a unique variable
in each macro.

Thanks for your help



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

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