Couldn't find this question on StackOverflow, so my apologies when not searched well enough...
I have the following code (simplified)
Sub Task_1()
"copy stuff from sheet2 to sheet1"
End Sub
Sub Task_2()
"Print sheet1"
End Sub
Sub Task_3()
"Do ordernumber 1"
End Sub
Now I want to loop this. So after sub Task_3, I want to call Task_1 again untill a certain cell is empty. I have the following, but not sure what to put into the questionmark.
Sub Start_orderprint()
Call Task_1
Call Task_2
Call Task_3
If Sheet1.Range("A4").Value <> Empty
Then ?????
Else
msgbox "Finished"
exit sub
End if
End sub
CodePudding user response:
A Do...Loop
to Run Procedures
Do...Loop statement
Using Do...Loop statements
- But be careful because this may end up being an endless loop (if it never becomes
Empty
).
Option Explicit
Sub Start_orderprint()
Do Until Sheet1.Range("A4").Value = Empty
' Or
'Do While Sheet1.Range("A4").Value <> Empty
Task_1
Task_2
Task_3
Loop
MsgBox "Finished"
End Sub
- In the previous example, if the value is initially
Empty
, the loop will never be entered. If you want to enter it once, no matter what, you will have to use the following:
Sub Start_orderprint()
Do
Task_1
Task_2
Task_3
Loop Until Sheet1.Range("A4").Value = Empty
' Or
'Loop While Sheet1.Range("A4").Value <> Empty
MsgBox "Finished"
End Sub