VBA Excel传递一个button作为参数
我想传递一个CommandButton作为参数。
例:
Sub calc(btn as button) btn.Caption = "Something" End Sub Private Sub CommandButton1_Click() calc(CommandButton1) End Sub Private Sub CommandButton2_Click() calc(CommandButton2) End Sub
是像上面的可能吗? 如果是,我该怎么办?
编辑
感谢您的回应,但我没有得到它。 所以现在看起来像这样:
Public Sub calc(ByRef btn as Object) btn.Caption = "Something" End Sub Private Sub CommandButton1_Click() calc(CommandButton1) End Sub Private Sub CommandButton2_Click() calc(CommandButton2) End Sub
也许有人可以更详细地向我解释,因为我很新的VBA。
这是sub:
Public Sub temp(ByRef cmdb As Object) cmdb.Caption = "somethine else" End Sub
这是你怎么称呼它
call sub (commandbutton_1)
你需要:
Sub calc(btn As MSForms.CommandButton) btn.Caption = "Something" End Sub
你必须按照规则调用它:
calc CommandButton1 // best call calc (CommandButton1) // ok but verbose calc (CommandButton1) // type mismatch!
(types不匹配是因为括号评估CommandButton1
导致其默认属性(一个string)与方法参数types不兼容)