我已经为这个问题苦苦挣扎了一段时间......我想做一些非常简单的事情。我想在运行时创建多个命令按钮,然后用一个过程处理这些命令按钮的事件。所以我建立了一个“withevents”类来处理自动化,但我的代码不起作用。当我运行 Test() 时,会创建命令按钮,但是当我单击它时...没有消息框响应...我找不到错误...请任何帮助都会很棒!
cTest 类
Public WithEvents Button As MSForms.CommandButton
Public Sub Button_Click()
s = MsgBox("Hello", vbOKOnly)
End Sub
模块 1
Public TestCollection As Collection
Sub Test()
Set TestCollection = New Collection
Dim Btn As CommandButton
Dim OLEBtnObj As cTest
Set OLEBtnObj = New cTest
Set Btn = Sheet1.OLEObjects.Add(ClassType:="Forms.CommandButton.1", link:=False,_ DisplayAsIcon:=False, Left:=368.25, Top:=51, Width:=44.25, Height:=24).Object
Set OLEBtnObj.Button = Btn
TestCollection.Add Item:=OLEBtnObj
End Sub