我想传递一个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很新。
答案 0 :(得分:3)
你需要:
Sub calc(btn As MSForms.CommandButton)
btn.Caption = "Something"
End Sub
您必须按照规则调用它:
calc CommandButton1 // best
call calc (CommandButton1) // ok but verbose
calc (CommandButton1) // type mismatch!
(类型不匹配是因为括号评估CommandButton1
导致其默认属性(字符串)与方法参数类型不兼容)
答案 1 :(得分:0)
这是子:
Public Sub temp(ByRef cmdb As Object)
cmdb.Caption = "somethine else"
End Sub
这就是你怎么称呼它
call sub (commandbutton_1)