VBA通过表单控件的function
所有,
我一直在努力这一段时间:是否有可能传递一个对象的function?
这是我想要完成的:
- 获取表单上按下哪个控件的名称(作为对象?)
- 发送控件的名称function“MyFunction”(作为参考?)
- 在“MyFunction”上禁用相同的控件
从form1调用:
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
button1_Click调用MyFunction并将调用者传递给它:
Private Sub MyFunction(caller As String) caller.Enabled = False End Sub
我明白这不会作为一个string 。 我怎么可能做到这一点呢? 谢谢!
把一个对象传递给一个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
在上面我创build了一个带有三个button的用户窗体,他们说他们是什么时候点击,然后被禁用。
注意
Disable CommandButton1
可以被replace
Disable Me.ActiveControl
甚至只是
Disable ActiveControl
你甚至可以像这样使用Variant(粗略的例子):
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
尽pipe约翰·科尔曼的例子比我的好。