I have two command buttons (cmd1
and cmd2
) on userform1
, when clicked each show the same userform (userform2
). Within the initialize
or load
sub is it possible to determine which command button was clicked on userform1
and therefore show the form differently? I imagine the code in either the initialize
or load
sub on userform2
to have the following skeleton:
if (cmd1 was clicked)
' do stuff relating to 1
elseif (cmd2 was clicked)
' do stuff relating to 2
else
' error handling
End if
The respective "stuff
" could be moved into the event handler for cmd1
and cmd2
however, if the method described above can be used it will be a lot simpler and cleaner.
Use a Public Variable
in UserForm1
and then test it in UserForm2_Initialize Event
.
Something like this in UserForm1:
Public whatsclicked As String
Private Sub CommandButton1_Click()
whatsclicked = "CommandButton1"
UserForm2.Show
End Sub
Private Sub CommandButton2_Click()
whatsclicked = "CommandButton2"
UserForm2.Show
End Sub
And then in UserForm2:
Private Sub UserForm_Initialize()
Select Case UserForm1.whatsclicked
Case "CommandButton1": MsgBox "CommandButton1 loaded form."
Case "CommandButton2": MsgBox "CommandButton2 loaded form."
Case Else 'Do something else
End Select
End Sub