Unload userform and show other userform but cleared

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
843
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
 
So I tested by opening userform2 again. That is when I got the error

My apologies having one of my senior days

Delete the QueryClose event completely in UserForm2

And update the CommandButton code In UserForm1 with following

VBA Code:
Private Sub CommandButton1_Click()
    UserForm1.Hide
    UserForm2.Show
    UserForm1.Show
End Sub

and see if this does what you want

Dave
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thanks Dave

I did get similar in a different thread
But with the clear code you posted above that seems to do everything I need although to close userform 1 I have to click the X twice

Ps what does hiding and showing userform1 do?
Does it hide1, show2 and show1 just in memory for when 2 closes

Post in thread 'Userform show / hide' Userform show / hide

VBA Code:
Private Sub CommandButton1_Click()
    Me.Hide
    UserForm2.Show
    Me.Show
End Sub
 
Upvote 0
... although to close userform 1 I have to click the X twice

If you wanted to prevent this behaviour, then you might consider not using the default instance of a UserForm class module, rather New-ing them up.
That way you're able to destroy the object within the same procedure were the object was created.
In addition, give your first UserForm a custom Show function, returning a value which indicates whether the form should be displayed on screen again.
Your code then could look like this:

This goes in a standard module:
VBA Code:
Public Sub LaunchForm1()

    Dim FormOne As UserForm1
    Set FormOne = New UserForm1

    Do While FormOne.ShowDialogAgain    ' <<< use custom function
    Loop

    Unload FormOne
    Set FormOne = Nothing
End Sub

Public Sub LaunchForm2()

    Dim FormTwo As UserForm2
    Set FormTwo = New UserForm2

    FormTwo.Show                        ' <<< use built-in method

    Unload FormTwo
    Set FormTwo = Nothing
End Sub

This goes in the module of UserForm1:
VBA Code:
Option Explicit

Private Type TLocals
    ShowAgain   As Boolean
End Type
Private this As TLocals

Private Sub ButtonShowForm2_Click()
    this.ShowAgain = True
    Me.Hide
    LaunchForm2
End Sub

Public Function ShowDialogAgain() As Boolean         '<<< custom function responsible for displaying the form
    Me.Show
    ShowDialogAgain = this.ShowAgain
End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VBA.vbFormControlMenu Then
        this.ShowAgain = False
        Cancel = True
        Me.Hide
    End If
End Sub

This goes in the module of UserForm2:
VBA Code:
Option Explicit

Private Sub ButtonClose_Click()
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VBA.vbFormControlMenu Then
        Cancel = True
        Me.Hide
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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