LOOP a macro based on cell value

c0087

Board Regular
Joined
Jul 13, 2015
Messages
94
Office Version
  1. 365
Platform
  1. Windows
Here is my code

Sub RunEight()


Call ThisWorkbook.formulaXXX
Call result1
Call ThisWorkbook.formulaXXX
Call result2
Call ThisWorkbook.formulaXXX
Call result3
Call ThisWorkbook.formulaXXX
Call result4

End Sub


After result4 is finished, AF22 will have a value. Unless the number is less than 2 OR greater than 8 I want SubRunEight to keep looping.

Thanks in advance
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about this?

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do Until .Value < 2 Or .Value > 8
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop
End With


End Sub

Make sure that that condition will be met at some point, otherwise your code will get caught in an infinite loop.

You can add DoEvents as below, which will let you break out of an infinite loop with Ctl+PageBreak, but it will impact performance.

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do Until .Value < 2 Or .Value > 8
        DoEvents
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop
End With


End Sub
 
Upvote 0
How about this?

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do Until .Value < 2 Or .Value > 8
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop
End With


End Sub

Make sure that that condition will be met at some point, otherwise your code will get caught in an infinite loop.

You can add DoEvents as below, which will let you break out of an infinite loop with Ctl+PageBreak, but it will impact performance.

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do Until .Value < 2 Or .Value > 8
        DoEvents
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop
End With


End Sub

I can't run it like that from the start because AF22 alternates values throughout, so I need it to wait until result4 finishes to then analyze AF22, then LOOP accordingly
 
Upvote 0
Does this fix the issue?

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do
        DoEvents
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop Until .Value < 2 Or .Value > 8
End With


End Sub
 
Upvote 0
Does this fix the issue?

Code:
Sub RunEight()
Dim r As Range: Set r = Range("AF22")


With r
    Do
        DoEvents
        Call ThisWorkbook.formulaXXX
        Call result1
        Call ThisWorkbook.formulaXXX
        Call result2
        Call ThisWorkbook.formulaXXX
        Call result3
        Call ThisWorkbook.formulaXXX
        Call result4
    Loop Until .Value < 2 Or .Value > 8
End With


End Sub

Yes sir - thank you
 
Upvote 0
───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂ ...
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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