art123456
art123456

Reputation: 137

VBA Pause code and wait for user interaction

I have code where I have added an error box that pops up based on certain conditions. The conditions are:

If Range (Q2:Q) = "" Then
ErrorFrame.Visible = True

When the error frame pops up, there are two options I want that I have added buttons for:

  1. Continue: Continue will continue with the macro regardless if there is data in column Q

  2. Stop: Stop will end the macro so the user can input the data into column Q and rerun the macro

I also would like it so that when the error frame pops up, the code is paused and does not continue yet until the user chooses one of the options.

Would anyone be able to give me an example of how this could work? I'm having trouble thinking of how to write this. Any help is much appreciated, thanks!

Upvotes: 1

Views: 14858

Answers (2)

Arturo
Arturo

Reputation: 11

If you trying to bring up a form the syntax should be the following:

frmMyForm.Show vbModal

The vbModal allows it to pause the current procedure and move onto the displayed form.

Upvotes: 1

art123456
art123456

Reputation: 137

Figured it out myself:

If WorksheetFunction.CountA(Range("Q:Q")) = 0 Then
UserForm1.ErrorFrame.Visible = True
    Do While UserForm1.ErrorFrame.Visible = True
    DoEvents
    Loop
    If ContinueFlag = True Then GoTo Line1
End If

Line1:

Basically for the continue button I just added a flag on it so when clicked it would make the flag true. Then in the function as you can see, when the flag is true it will go to Line 1, which then continues the code and exits the Do While loop.

May not be the most efficient way, I'm sure, but let me know what you guys think and if there is a better way to do it.

Upvotes: 4

Related Questions