How to go back or re-display Userform after hiding it?

11,098

Solution 1

I think the problem is the order of the statements. I found out by using the debugger that when I had the Show statements before the Hide or Unload, these last are not executed.

Try this

' on UserForm2
Private Sub CommandButton1_Click()
    Me.Hide
    UserForm1.Show
End Sub

' on UserForm1
Private Sub CommandButton1_Click()
    Me.Hide
    UserForm2.Show
End Sub

Solution 2

Change to this:

Private Sub CButton1_Click()
   Me.Hide
   UserForm1.Show
   Unload Me
End Sub

Private Sub CButton2_Click()
   Me.Hide
   UserForm2.Show
   Unload Me
End Sub
Share:
11,098
L42
Author by

L42

Worked as a process engineer for a long time and eventually as an analyst. To make things easier, I do MS office automation through vba (eg. create add-ins, UDF and other cool stuff) I mostly work on Excel so you'll see me around working on questions tagged excel,vba and excel-formula. I also work on sql-server-2008 and sql-server-2012 but there are a lot of people in this site much better than me on these tags so I prefer learning from them instead :)

Updated on June 13, 2022

Comments

  • L42
    L42 almost 2 years

    I have this code in one command button in UserForm2:

     Private Sub CButton1_Click()
         UserForm1.Show
         Me.Hide
     End Sub
    

    Now, Userform1 is shown.
    Then I have another code in one command button in Userform1:

     Private Sub CButton2_Click()
         UserForm2.Show
         Unload Me
     End Sub
    

    This throws up a:

    Runtime Error: Form already displayed; can't show modally

    How do I do this properly?
    How do I go back to the previous Userform after hiding or unloading it?

  • Admin
    Admin over 10 years
    the only problem with unloading its that you loose the values filled on a form.. hidding is more appropriate
  • Octavio
    Octavio over 10 years
    @mehow yeah, you are right. I changed my code according to your observation.
  • L42
    L42 over 10 years
    thanks, it works. sequence in coding is really important when working with Userforms i guess.
  • L42
    L42 over 10 years
    thanks mate. this works too. but i have to give credit to Octavio for the explanation. :)