I have a userform with a button on it. I want the button to "refresh" the form, in other words I want the form to end, and then reopen.
The code below won't work because the form/macro has already ended, but I want the button to perform this task or similar.
sub command1.click()
end
userform1.show
end sub
I have checked and tried most or all the options I can choose from for a userform to "refresh". Is this even possible?
Here is a very simple way. Simply Re-Initialze the Userform using the UserForm_Initialize
Private Sub UserForm_Initialize()
TextBox1.Text = "" '<~~ Just an Example
'
'~~> Put here the code to re-initialize the userform which will refresh it
'
End Sub
Private Sub CommandButton1_Click()
TextBox1.Text = "Sid"
MsgBox "Re-Initialzing the Userform"
UserForm_Initialize
End Sub
Private Sub CommandButton1_Click()
Unload Me
UserForm1.Show
End Sub
Related
I have 2 Userforms, Userform1 and Userform2. I would to like to code Userform2 so that when the user closes it using the X button on the top right corner, it closes and Userform1 is automatically opened. The user can go from Userform1 to Userform2 with a CommandButton that has the following code:
Private Sub CommandButton1_Click()
Unload Me
UserForm2.Show
End Sub
After closing Userform2, I managed to automatically open Userform1 with the following code in it:
Private Sub UserForm_Terminate()
UserForm1.Show
End Sub
The problem is that this only works once. The second time the user clicks CommandButton1, Userform1 is closed and Userform2 showed, but Excel hangs up. The only way to fix it is to stop all the code from the developer environment or force quit Excel.
Using the UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) event isn't possible because that way, Userform2 never reaches the Unload part and is just kept in the background while Userform1 launches again, like this:
There is no need to unload UserForm1. Simply hide it. Also there is no need to show UserForm1 from UserForm2. It will automatically get shown when UserForm2 unloads. Is this what you are trying?
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide '<~~ Hide the form
Dim frmTwo As New UserForm2
'~~> Show the form in Modal(default)
frmTwo.Show
'~~> Show UserForm1 when UserForm2 unloads
Me.Show
End Sub
EDIT
Thank you for taking the time to help me! Unfortunately this is not what I am looking for. I'm specifically trying to unload Userform1 when showing Userform2 and viceversa because I don't want/need to preserve any data. I know manually clearing the form would also work, but the other thing is that this is only an example. My real code has many forms and I don't want all of them to remain loaded in the memory. That could potentially cause some performance issues. – Fernanda 10 mins ago
This doesnt crash for me
In a module
Option Explicit
Sub Sample()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
In Userform1
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
Dim frmTwo As New UserForm2
frmTwo.Show
End Sub
In Userform2
Option Explicit
Private Sub UserForm_Terminate()
Dim frmOne As New UserForm1
frmOne.Show
End Sub
I created two forms. Pressing the button 1 opens the form number 2. By closing the form number 2, the form number 1 is displayed. But this action is only done once and it stops for the second time and almost does not work. Where does the code have a problem?
code Userform1:
Private Sub ShowUserform2_Click()
UserForm1.Hide
Unload UserForm1
UserForm2.Show
End Sub
Code userform2:
Private Sub UserForm_Terminate()
UserForm2.Hide
Unload UserForm2
UserForm1.Show
End Sub
Skip the formName.Hide lines. They are unnecessary.
After the Unload formName statements add:
Set formName = Nothing
Also, make the otherForm.Show line precede the above two lines.
Try this code:
code Userform1:
Private Sub ShowUserform2_Click()
UserForm1.Hide
UserForm2.Show
End Sub
Code userform2:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
UserForm1.Show
End Sub
I am using a userform and have issue with the "unload me"
If I place the "unload me" before my private sub => the private sub doesn t start
If I place it after, I cannot click in the cell of the new workbook (which is what I see first on my screen when the macro is done)
Where should I put the unload me to make it work properly?
Code below:
Private Sub CommandButton3_click()
Private_Sub
Unload Me
End Sub
Private Sub Private_Sub()
Workbooks.Open Filename:="U:\mydocuments\Workbook.xlsx"
End Sub
Please tell me if I don t make myself clear enough.
I have four modules, three buttons and one userform, a progressbar, in my Excel workbook. I would like to show a progressbar during the runtime of all four modules.
Example:
I click on a button which executes the following code and makes my progressbar visible:
Private Sub GWPCClearDataButton_Click()
ProgressBar.Show
End Sub
In my userform i have the following code:
Private Sub UserForm_Activate()
GWPCClearData
End Sub
This calls one of my four modules which works fine so far.
Now here's where I'm stuck.
Of course, I would like to use the same progressbar for all modules but how can I determine in the userform code block which button was clicked and then depending on that call another module?
Example:
Private Sub GWPCClearDataButton_Click()
ProgressBar.Show
End Sub
Private Sub GSEPClearDataButton_Click()
ProgressBar.Show
End Sub
UserForm:
Private Sub UserForm_Activate()
If "BUTTON_NAME" = "GWPCClearDataButton" Then
GWPCClearData
ElseIf "BUTTON_NAME" = "GSEPClearDataButton" Then
GSEPClearData
End if
End Sub
How could I do that? I have yet to find a possibility to pass a parameter to my userform which would contain the button name.
Thanks for any help.
In your form put
Sub Start_Form(Called_From as string)
'Process Called_From as needed
End Sub
When you call your form just call the sub first like below
<form_name>.Start_Form "Button Called From"
<form_name>.Show
Basically you have to pass the variable via another sub(can be called anything) in the userform and then show the form after
User Form Code
Dim test
Private Sub UserForm_Activate()
MsgBox test
End Sub
Sub start(tt)
test = tt
End Sub
Module code
Sub t()
UserForm1.start "Hello World!"
UserForm1.Show
End Sub
In excel-vba i have a form named frmTemplateSelector when I select one of the two options and click on "done", the form doesn't hide!
I am calling frm.show from another module, where in depending on choice, other pop ups will appear.
Private Sub btnDone_Click()
frmTemplateSelector.Hide
Exit Sub
End Sub
Private Sub btn1_Click()
name = "XYZ"
End Sub
Private Sub btn2_Click()
name = "ABC"
End Sub
You can either Unload or Hide an UserForm.
Hide literally only hide the UF
Unload hide and reset all variables in the UF
Private Sub btnDone_Click()
Unload frmTemplateSelector 'or Unload Me
Exit Sub
End Sub
You can also change the UF's name to Me in this code, as it is the UF's code module, like this :
Private Sub btnDone_Click()
Me.Hide
Exit Sub
End Sub