Home > Blockchain >  Continue loop sub1 to sub 3 in VBA
Continue loop sub1 to sub 3 in VBA

Time:11-04

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

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
  • Related