确定单击了哪个命令按钮以打开userform

时间:2022-12-14 19:36:03

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:

我在userform1上有两个命令按钮(cmd1和cmd2),单击时每个按钮显示相同的用户窗体(userform2)。在initialize或load子中,是否可以确定在userform1上单击了哪个命令按钮,从而以不同的方式显示表单?我想象userform2上的initialize或load子中的代码具有以下骨架:

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.

可以将相应的“内容”移动到cmd1和cmd2的事件处理程序中,但是,如果可以使用上述方法,则会更加简单和清晰。

2 个解决方案

#1


2  

Use a Public Variable in UserForm1 and then test it in UserForm2_Initialize Event.
Something like this in UserForm1:

在UserForm1中使用公共变量,然后在UserForm2_Initialize事件中测试它。 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:

然后在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

#2


0  

you can do it also without public variable.

你也可以没有公共变量。

i won't show an example where you can simply write something in a cell or hidden sheet, instead i just pass the wanted info directly .

我不会展示一个例子,你可以简单地在单元格或隐藏的工作表中写一些东西,而不是直接传递想要的信息。

this time whatsclicked is the name of a label in userform2,

这次whatsclicked是userform2中标签的名称,

in userform1, before calling userform2:

在userform1中,在调用userform2之前:

Private Sub CommandButton1_Click()
load UserForm2
with UserForm2
    .whatsclicked.caption= "CommandButton1"
    .Show
end with
End Sub

Private Sub CommandButton2_Click()
load UserForm2
with UserForm2
    .whatsclicked.caption= "CommandButton2"
    .Show
end with
End Sub

Of course, you will have to hide the text of whatsclicked to the user (same color as font or background...)

当然,你必须隐藏用户的文字(与字体或背景相同的颜色......)

#1


2  

Use a Public Variable in UserForm1 and then test it in UserForm2_Initialize Event.
Something like this in UserForm1:

在UserForm1中使用公共变量,然后在UserForm2_Initialize事件中测试它。 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:

然后在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

#2


0  

you can do it also without public variable.

你也可以没有公共变量。

i won't show an example where you can simply write something in a cell or hidden sheet, instead i just pass the wanted info directly .

我不会展示一个例子,你可以简单地在单元格或隐藏的工作表中写一些东西,而不是直接传递想要的信息。

this time whatsclicked is the name of a label in userform2,

这次whatsclicked是userform2中标签的名称,

in userform1, before calling userform2:

在userform1中,在调用userform2之前:

Private Sub CommandButton1_Click()
load UserForm2
with UserForm2
    .whatsclicked.caption= "CommandButton1"
    .Show
end with
End Sub

Private Sub CommandButton2_Click()
load UserForm2
with UserForm2
    .whatsclicked.caption= "CommandButton2"
    .Show
end with
End Sub

Of course, you will have to hide the text of whatsclicked to the user (same color as font or background...)

当然,你必须隐藏用户的文字(与字体或背景相同的颜色......)