0
votes

I load a UserForm, do some stuff, then want to hide that UserForm and show one of the open worksheets. This worksheet has a command button to run a macro that will again show the UserForm. What happens is that when I hide the UserForm, the ActiveSheet becomes the one which was active when the UserForm was loaded. Subsequent attempts at defining the active sheet are ignored, with no error messages.

UserForm1.Show
' stuff
Workbooks("datafile").Sheets(1).Activate   ' ActiveSheet is correctly redefined
UserForm1.Hide   ' ActiveSheet changes to the active sheet when UserForm was loaded
Workbooks("datafile").Sheets(1).Activate      ' Ignored.  no change to ActiveSheet
' Shows whomever loaded the user form: either the VBA debugger or the original worksheet

2

2 Answers

0
votes

I'm not able to recreate this issue, the posted code worked for me.

however, the UserForm1 might be overlapping with the following instruction, try adding DoEvents function before activating the desired sheet again. something like this:

UserForm1.Show
' stuff
Workbooks("datafile").Sheets(1).Activate   ' ActiveSheet is correctly redefined
UserForm1.Hide   ' ActiveSheet changes to the active sheet when UserForm was loaded
DoEvents
Workbooks("datafile").Sheets(1).Activate
0
votes

It works out that a User Form is linked to the worksheet that was active when it was loaded. Thus, if you want a particular sheet to be visible when you hide or minimize the form, make sure it was Active when you loaded that form. It appears you cannot change ActiveSheets while a UserForm is loaded; you're stuck with the sheet that was there when the form was loaded.