Travis
Travis

Reputation: 13

How to exit multiple subs in Word VBA

I am using VBA. I have a Word doc that launches a userform upon opening. The userform accepts loads of input from the user (via text boxes, combo boxes, check boxes, and list boxes) and then a command button at the bottom of the userform is clicked. Executed code then takes all the input from the userform, processes the information, and fills the appropriate data in several bookmarks in the document.

I usually don't pay much attention to error correction and consequently my macros crash if I don't do everything right (I'm just an after-hours noob doing personal projects). I'm trying to implement best practices and so I want to build in some error handling code. In particular, I want the user to be informed if a data field on the userform is skipped, and processing of the script to stop.

I can detect if there is an omission and notify the user with a message box and exit the immediate sub routine. I can't figure out how to exit two subroutines. I'm not including actual code because I'm hoping someone can just point me in the right direction conceptually, rather than write my code for me.

My current flow is such: Command button is clicked and the sub routine goes through each input field in the userform. For each input field, it calls another sub routine to grab the values from the input field. That sub routine then calls another subroutine to update the appropriate bookmark. Then control returns to the main sub routine that is called by the command button and the next input field is processed.

I check for empty field/omitted response in the second subroutine which grabs the value for the field. Using an If statement I can launch the message box and exit the sub if there is a null value. But that only exits the secondary sub; the main sub that was launched when the command button was clicked continues to run and process the remaining input fields. I want to stop this process as well, so that the user has to click the command button again after fixing the error. I don't want to do a terminal end command because that will clear the userform and I'll have to start from scratch. How can I stop both running sub routines without shutting the whole thing down?

Thanks in advance,

Upvotes: 1

Views: 207

Answers (1)

trashrobber
trashrobber

Reputation: 777

You could change your second subroutine into a function that returns a bool value for success or failure, then in your main routine exit if it is a failure.

Upvotes: 0

Related Questions