Multiple loops in one macro

Ejimenez

New Member
Joined
Nov 10, 2012
Messages
14
Can you have multiple loops in one macro that does different things. If so, how can you end one loop to start the other loop.. I tried using the work loop but it gives me an error.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Exit Do get you out of a Do While/Until loop
Exit For geys you out of a For/Next loop

Several looping examples:
Code:
Sub Foo()

    x = 0
    l = 1
    Do
        x = x + 1
        DoEvents
    Loop While x < 10
    MsgBox "Value: " & x, vbOKOnly, "Loop: " & l & " results"
    
    l = l + 1
    Do
        x = x + 1
        DoEvents
    Loop Until x > 15
    MsgBox "Value: " & x, vbOKOnly, "Loop: " & l & " results"

    l = l + 1
    Do Until x > 19
        x = x + 1
        DoEvents
    Loop
    MsgBox "Value: " & x, vbOKOnly, "Loop: " & l & " results"
    
    l = l + 1
    Do While x > 10
        x = x - 1
        If x = 12 Then Exit Do
        DoEvents
    Loop
    MsgBox "Value: " & x, vbOKOnly, "Loop: " & l & " results"

End Sub
 
Upvote 0
Here are three types of loops. You can find a lot of information on the web in free tutorials. Just type in "VBA loops tutorial" in your browser and enter.

Code:
Dim c As Range
For Each c In Range("A2:A5")
If c <> "" Then
MsgBox "Something There"
Else
MsgBox "Blank"
End If
Next
Code:

For Specified value loop:

Code:
Dim i As Long
For i = 2 To 5
If ActiveSheet.Cells(i, 1) <> "" Then
MsgBox "Something There"
Else
MsgBox "Blank"
End If
Next
Code:

Do Loop

Code:
Dim Counter As Long
counter = 2
Do While counter < 6
If Range("A" & counter) <> "" Then
MsgBox "Something There
Else
MsgBox "Blank"
End If
Loop
Code:

All three do the same thing. Each one is limited in the number of time is will loop by the parameters in the For or Do line of the code. Each one will only check cells A2:A5 for a value and display a message box based on what it finds. You can run loops consecutively or you can nest them. A Nested loop would be like:

Code:
Dim r As Range
For Each r In Range("A2:A5")
r = 1 + r.Offset(-1, 0).Value
For i = 2 To 5
ActiveSheet.Cells(i, r + 1) = r.Value + 1
Next
Next
Code:
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top