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
Related
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 have a Problem with a Userform which I called "ComboTest2". It only consists of two Comboboxes. If I instantiate the USerform as an object then the following Code doesn't work in the sense that the second combobox of the Userform doesn't contain the desired data.
Sub FillCombo(ByVal row As Long)
Dim rgCities As Range
Set rgCities = Worksheets("Tabelle2").Range("B2:D2").Offset(row)
ComboTest2.ComboBox2.Clear
ComboTest2.ComboBox2.List = WorksheetFunction.Transpose(rgCities)
ComboTest2.ComboBox2.ListIndex = 0
End Sub
Sub FillMain()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
ComboForm2.Show
End Sub
UserForm-Code:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Hide
End Sub
Private Sub ComboBox1_Change()
FillCombo ComboBox1.ListIndex
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Worksheets("Tabelle2").Range("A2:A5").Value
ComboBox1.ListIndex = 0
FillCombo ComboBox1.ListIndex
End Sub
But if I use the "default instantiation" by VBA which means that I change the FillMain Sub to:
Sub FillMain2()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
'ComboForm2.Show
ComboTest2.Show
End Sub
Then everything is fine. Why is that so?
Best regards
It's because FillCombo is referring to the userform by name, and therefore to the default instance (you're actually creating a new instance of the form). If that code is not in the userform, and I'm not sure why you would have it outside the form, you should pass the combobox as an argument to it.
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
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