Question about modeless forms

TomCon

Active Member
Joined
Mar 31, 2011
Messages
385
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
What i want to do is have a modelss form Show within a macro and then when the user presses a "continue" button, for the macro to continue from the next line after the form was shown (Show method). Basically i want to give some time to use the Excel UI to look at and inspect a few things, and then if all is ok, continue the macro, or else say "Cancel" and end it. But in my experiments, modeless forms do not seem to work that way. They seem to just be a branch to the Form code, and not return to the macro that called them. Is that so? Is there a way to do what i described? My first cut is that i would just copy the rest of the code in the macro to the Continue button handler, but that seems awkward and also there are scope issues, i'd have to get all the variable values that the remainder of the macro depends on over to the Continue button handler.

Is there any way to do what i described? Just "pause the code of the macro" during the display of the modeless form, and then resume the same macro at the same point as the form was shown, when the user indicated to do so?

Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
With the following code, the main sub calls the showForm method of the userform. The showForm method then shows the userform as modeless, and then enters into a loop until the user clicks on the button. Once the user clicks on the button, the userform is unloaded, and the remainder of the code within the main sub gets executed. Change the name of the controls accordingly.

[UserForm code]

VBA Code:
Option Explicit

Dim continue As Boolean

Public Sub showForm()

    Me.Show 0

    Do
        DoEvents
    Loop Until continue = True
    
    Unload Me

End Sub

Private Sub CommandButton1_Click()

    continue = True
    
End Sub

Private Sub UserForm_Initialize()

    continue = False

End Sub

[Regular module]

VBA Code:
Option Explicit

Sub main()

    UserForm1.showForm
    
    MsgBox "Goodbye!"
    
End Sub

Hope this helps!
 
Upvote 0
Solution
Yes, quite amazing. Thank you very much. This code does what i need...and a useful technique to understand!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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
Back
Top