LOOP a macro based on cell value

c0087

Board Regular
Joined
Jul 13, 2015
Messages
55
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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,439
Office Version
  1. 365
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

Board Regular
Joined
Jul 13, 2015
Messages
55
Office Version
  1. 365
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

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
3,439
Office Version
  1. 365
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

Board Regular
Joined
Jul 13, 2015
Messages
55
Office Version
  1. 365
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

Yes sir - thank you
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,439
Office Version
  1. 365
Platform
  1. Windows
───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂ ...
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,629
Messages
5,838,453
Members
430,549
Latest member
jayjay2022

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
Top