Very basic questions about VBA UserForm

TomCon

Active Member
Joined
Mar 31, 2011
Messages
397
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Very basic "getting started with UserForm type questions!

(1) Is it so that once you have a Form.Show statement in VBA, it is a permanent branch away from the Sub that shows the form, and further code in that Sub will never be executed? All subsequent code you would want to execute then must be within the button handler?

Here is a picture of code in a button handler.
1744532725918.png


(2) It appears to me that breakpoints are not honored within the handler for the button click. Is that true? In the code above, code never stops at a breakpoint. The first MsgBox statement is executed and the MsgBox appears. The code does not break before the MsgBox (nor after it).

(3) After the message "in ok button" appears and i click OK to the MsgBox, the next statements are not executed. I am simply returned to the VBA editor, and execution has ended. The Call to NewSub and the second MsgBox are never executed. What is going on? How do i get more code to execute?

Thanks much! I know this is very basic. Just trying to get the lay of the land with UserForms and understand where the execution flow picks up next.
 

Attachments

  • 1744532571757.png
    1744532571757.png
    8.2 KB · Views: 4
I don't know if I understand your question, but I'll try...
VBA Code:
Sub test()
Dim s As String
    MsgBox "hic hic"    '   (A)
    UserForm1.Show
    s = InputBox("blala")
End Sub

You already know that the UserForm1.Show line will be executed only when the MsgBox (A) window is closed. So similarly, the s = InputBox("blala") line will be executed only when the UserForm1 window is closed. But unlike with MsgBox, you can display UserForm1 differently.
a.
UserForm1.Show (or UserForm1.Show True - by default Modal = True)
--> the s = InputBox("blala") line will be executed only when the UserForm1 window is closed
b.
UserForm1.Show False
--> the s = InputBox("blala") line will be executed immediately.
 
Upvote 0
Solution
I don't know if I understand your question, but I'll try...
VBA Code:
Sub test()
Dim s As String
    MsgBox "hic hic"    '   (A)
    UserForm1.Show
    s = InputBox("blala")
End Sub

You already know that the UserForm1.Show line will be executed only when the MsgBox (A) window is closed. So similarly, the s = InputBox("blala") line will be executed o,nly when the UserForm1 window is closed. But unlike with MsgBox, you can display UserForm1 differently.
a.
UserForm1.Show (or UserForm1.Show True - by default Modal = True)
--> the s = InputBox("blala") line will be executed only when the UserForm1 window is closed
b.
UserForm1.Show False
--> the s = InputBox("blala") line will be executed immediately.
Thanks much. You have clarified the code flow for me.

I was about to post about a remaining issue, that all breakpoints are being ignored and also if i start code execution with F-8, expecting to step line by line, the code just executes, does not stop. I thought it was related to a possible misuse of UserForm. But i first tested by closing Excel and restarting. And now breakpoints are working and F-8 goes line by line. So, it looks like for some reason Excel was in a state where that was being ignored.

Thanks for your help!!!
 
Upvote 0
Thanks much. You have clarified the code flow for me.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

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