My question is, when I call that function ("MyFunc") from somewhere, will the click event of the button ("cmdButton") continue processing before the function is allowed to continue, or is it basically a coin toss?
Let me explain it with an image followed by a description
- When you run the function, the first line is executed. Now since the form is shown as modal, the next line which is
stringVar = "Second"
will not run till the time the userform is unloaded. - The next line which will execute in the above scenario is
Unload Me
. However, it doesn't mean that any code after that will not run.Unload Me
is not likeExit Do
orExit For
. It just unloads the object and in no way stop further execution of the script/code. - So obviously the next line which is executed is
stringVar = "First"
and it reachesEnd Sub
- This is the time the control has been returned to the function and it continues to execute the next line which is
stringVar = "Second"
Hope this makes sense. If it doesn't then please feel free to ask whatever doubts that you have.
You can also see the execution of the code by stepping through it using F8.
For further reading: Debugging VBA Code