LOOP a macro based on cell value

c0087

New Member
Joined
Jul 13, 2015
Messages
43
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:

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,848
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
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
 

c0087

New Member
Joined
Jul 13, 2015
Messages
43
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
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,848
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
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
 

c0087

New Member
Joined
Jul 13, 2015
Messages
43
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
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,848
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂ ...
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 

Watch MrExcel Video

Forum statistics

Threads
1,109,518
Messages
5,529,314
Members
409,862
Latest member
lbisacca
Top