Excel VBA将窗体控件传递到函数
全部, 我已经为此挣扎了一段时间:是否可以将对象传递给函数 以下是我试图实现的目标:Excel VBA将窗体控件传递到函数,excel,vba,Excel,Vba,全部, 我已经为此挣扎了一段时间:是否可以将对象传递给函数 以下是我试图实现的目标: 获取窗体上按下的控件的名称(作为对象?) 将控件的名称发送到函数“MyFunction”(作为参考?) 禁用“MyFunction”上的相同控件 从表单1调用: Private Sub button1_Click() Dim caller As String caller = Form1.ActiveControl.Name MyFunction(caller) End Sub 'I
Private Sub button1_Click()
Dim caller As String
caller = Form1.ActiveControl.Name
MyFunction(caller)
End Sub 'I'm able to pass it as a string
按钮1\u单击调用MyFunction并将调用方传递给它:
Private Sub MyFunction(caller As String)
caller.Enabled = False
End Sub
我知道这不能作为字符串使用。我怎么可能作为一个实际的对象来做呢?
谢谢大家! 将对象传递给子对象几乎没有问题:
Private Sub Disable(c As Control)
MsgBox c.Name
c.Enabled = False
End Sub
Private Sub CommandButton1_Click()
Disable CommandButton1
End Sub
Private Sub CommandButton2_Click()
Disable CommandButton2
End Sub
Private Sub CommandButton3_Click()
Disable CommandButton3
End Sub
在上面我创建了一个带有三个按钮的用户表单,它们在单击时表示自己是谁,然后被禁用
注意
Disable CommandButton1
可以用
Disable Me.ActiveControl
甚至只是
Disable ActiveControl
您甚至可以像这样使用变体(粗略示例): 不过,约翰·科尔曼的例子比我的好
Private Sub CommandButton1_Click()
EnableDisable ActiveControl, "disable"
End Sub
Private Sub EnableDisable(control As Variant, status As String)
If status = "enabled" Then
control.Enabled = True
Else
control.Enabled = False
End If
End Sub