Resume main Sub Code once UserForm1 has been completed

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
433
Hi
Looking for your help please.
I have some code that runs then it loads a UserForm1 which has some checkboxes on it and 2 buttons a cancel (which just ends it) and an OK which protects the sheet without a password and unloads the userform.

This all works fine

The issue I have is I need to go back to the main VBA code once OK is clicked to resume the rest of the code. I cannot seem to find a way to do this I tried a GoTo statement in the OK button code but because the Line was in the main Sub code it couldnt find it.

Can anyone suggest how I can get it to go from the 'OK' button and resume the main code please? Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You won't be able to resume code from the middle of module, you can only start it from one place. What you need to do is think about it in a different way: think about your whole process as a series of separate steps, each has to be done in a clearly defined order. To determine where you are in the series of steps you need to keep a record what step you are currently on. This processs is easily controlled by a bit of code that is a "state machine" This is code that goes through a series of "states" in a defined sequence , the transition from one state to the next is done by a trigger> So I suggest you write down the different steps you need to make and work out what is the triger that will move from one to the next.
So from what you have said:
Inital state: Running 1st Macro, trigger: Opening Userform, new state: Userform open, trigger User form close, New state running 2nd macro.
You can keep the "state" of your statemachine in a global variable which will persist while you workbook is open, or you can keep in in a cell on the workbook
 
Upvote 0
Thanks offthelip, I do have all my steps outlined its just that once the Form has been run I still need to run some more code and I cant do the form last.

Could I therefore put my last piece of code from the main module into its own Sub and then when the form OK button is pressed asl it to call that sub up to continue?
 
Upvote 0
Solution
Thanks offthelip, I do have all my steps outlined its just that once the Form has been run I still need to run some more code and I cant do the form last.

Could I therefore put my last piece of code from the main module into its own Sub and then when the form OK button is pressed asl it to call that sub up to continue?
I did this and it has done what I needed. Thanks
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,250
Members
453,026
Latest member
cknader

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