ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Do loops within Do loops (https://www.excelbanter.com/excel-programming/324458-do-loops-within-do-loops.html)

Linking to specific cells in pivot table

Do loops within Do loops
 
Hi - I've written code that has 1 loop within another loop that is within yet
another loop - looks something like this:

Do
Do
Do
Loop
Loop
Loop

It is not working though...just wanted to know if in theory a loop of this
sort should work (assuming all other coding is correct).

Thanks!

Jim Thomlinson[_3_]

Do loops within Do loops
 
Yup that format is fine in general. The only problem is that it can be a
little tough to debug. That having been said there is often no good way
around it. Check to ensure that each loop has an appropriate stop condition
that can be met regardless of where it is in the processing of all of the
loops... That is usually were the problem is and you end up in an infinite
loop.

HTH

"Linking to specific cells in pivot table" wrote:

Hi - I've written code that has 1 loop within another loop that is within yet
another loop - looks something like this:

Do
Do
Do
Loop
Loop
Loop

It is not working though...just wanted to know if in theory a loop of this
sort should work (assuming all other coding is correct).

Thanks!


Linking to specific cells in pivot table

Do loops within Do loops
 
Thanks Jim!

"Jim Thomlinson" wrote:

Yup that format is fine in general. The only problem is that it can be a
little tough to debug. That having been said there is often no good way
around it. Check to ensure that each loop has an appropriate stop condition
that can be met regardless of where it is in the processing of all of the
loops... That is usually were the problem is and you end up in an infinite
loop.

HTH

"Linking to specific cells in pivot table" wrote:

Hi - I've written code that has 1 loop within another loop that is within yet
another loop - looks something like this:

Do
Do
Do
Loop
Loop
Loop

It is not working though...just wanted to know if in theory a loop of this
sort should work (assuming all other coding is correct).

Thanks!


Nicole B

Do loops within Do loops
 
Would someone give an example of how to use a Stop condition? I'm still
working on conceptualizing vb[a].

"Jim Thomlinson" wrote:

Yup that format is fine in general. The only problem is that it can be a
little tough to debug. That having been said there is often no good way
around it. Check to ensure that each loop has an appropriate stop condition
that can be met regardless of where it is in the processing of all of the
loops... That is usually were the problem is and you end up in an infinite
loop.

HTH


JE McGimpsey

Do loops within Do loops
 
Something needs to tell your code to stop looping - for example, it may
be a counter:

Dim nCtr As Long
nCtr = 0
Do
nCtr = nCtr + 1
Loop Until nCtr 100

the "Until nCtr 100" is your "stop condition". Or you can use an Exit
statement:

Const sPWORD As String = "drowssap"
Dim vResponse As Variant
Do
vResponse = Application.InputBox("Enter the Password")
If vResponse = False Then Exit Sub 'user cancelled
If vResponse = sPWORD Then Exit Do
MsgBox "That's not correct"
Loop

there are a number of other conditions you can use. See the "Using
Do...Loop Statements" topic in VBA Help.

Note that you need to be careful to avoid an infinite loop. For
instance, this will never exit:

Dim nCtr As Long
nCtr = 1
Do
nCtr = nCtr + 2
Loop Until nCtr = 100



In article ,
"Nicole B" wrote:

Would someone give an example of how to use a Stop condition? I'm still
working on conceptualizing vb[a].



All times are GMT +1. The time now is 03:36 AM.

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