Unload userform and show other userform but cleared

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
832
Office Version
  1. 365
Platform
  1. Windows
Have 2 userforms called userform1 and userform2

When I have userform1 opened I sometimes need to work on userform2 so Use

VBA Code:
Userform1.Hide
Userform2.Show

However when finished with userform2 I want to be able to click a command button to unload userform2 and reshow userform

I've used
VBA Code:
Unload userform2
Userform1.show

But when userform1 is shown, I want it to be cleared

Also can same be done when I click the cross on userform2, which will unload userform2 and show userform1 but cleared
 

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.
If you want UserForm1 to be cleared for each use, then when you close it you can use

VBA Code:
Unload Userform1

instead of

VBA Code:
Userform1.Hide

Hiding a userform simply closes it from display, but unloading it takes it out of memory so the next time it is displayed everything is in the default state.
 
Upvote 0
But I wanted to use some information from userform1 while using userform2. Then when finished with userform2 unload that, and userform1.show

I thought .show would clear it but it doesn't

Also how can I show userform1 if user clicks on the x on userform2
 
Upvote 0
You will have to write

VBA Code:
Public Sub UserForm_Activate()

End Sub

for UserForm1 that clears the contents each time that Show is called. I can't write it because I don't know what's in UserForm1.

Add this sub to UserForm2 to show UserForm1 when UserForm2 is closed:
VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   UserForm1.Show
End Sub
 
Upvote 0
Thanks
I'm assuming userform activate is with rest of userform1 code I.e.

VBA Code:
Public Sub UserForm_Activate()
' clear details in userform 1
End Sub

Also code below showed userform 1 but userform 2 also stayed open

Did add unload userform2 before show but both were still open

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   UserForm1.Show
End Sub
 
Upvote 0
But when userform1 is shown, I want it to be cleared

Try Following in UserForm1

VBA Code:
Private Sub UserForm_Activate()
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        Select Case TypeName(Ctrl)
        Case "TextBox", "ComboBox"
            Ctrl.Value = ""
        Case "CheckBox", "OptionButton"
            Ctrl.Value = False
        End Select
    Next Ctrl
End Sub

Also can same be done when I click the cross on userform2, which will unload userform2 and show userform1 but cleared

and this in UserForm2

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   UserForm1.Show
End Sub

and see if these codes will do what you want

Dave
 
Upvote 0
Thanks

The activate code works ????

When I entered code below in userform2, when I closed using the X it showed userform1 in the background.

Then when I ran code from userform1 to show userform2 I got error 400 form already displayed, can't show modally

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   UserForm1.Show
End Sub
 
Upvote 0
Userform2 is opened from command button in userform 1
Userform1.hide
Userform2.show ' that's what the error above highlights when I try and reopen it
 
Upvote 0
I stated that ONLY to place the code for QueryClose Event in UserForm2

Dave
 
Upvote 0
I only entered query close in userform2

I opened uuserform1 then opened userform2 using a command button in userform1

I then clicked the X in userform2 which showed userform1 but userform2 was also shown in the background

So I tested by opening userform2 again. That is when I got the error
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,265
Members
449,149
Latest member
mwdbActuary

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