我正在尝试创建一个非常简单的自定义事件处理程序
Public Event wshActivate(ID As Long, ByRef cancel As Boolean)
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim numba As Long: numba = 10
Dim cancel As Boolean: cancel = False
RaiseEvent wshActivate(numba, cancel)
End Sub
我没有得到的是我应该如何定义WithEvents监听器?我怎么知道这个?
答案 0 :(得分:1)
我不知道你的意图,但我根据你的样本创造了一些有用的东西。首先,您需要一个名为Class1
的类模块。
在ThisWorkbook模块中,我有以下代码:
Private WithEvents cls As Class1
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Set cls = New Class1
cls.ImportantFunctionFromClass
End Sub
Private Sub cls_wshActivate(ID As Long, cancel As Boolean)
MsgBox "this is a msg from an event inide the ImportantFunction"
End Sub
的Class1:
Public Event wshActivate(ID As Long, ByRef cancel As Boolean)
Sub ImportantFunctionFromClass()
Dim numba As Long: numba = 10
Dim cancel As Boolean: cancel = False
'...some relevant code...
RaiseEvent wshActivate(numba, cancel)
'...some relevant code...
End Sub
您可以看到我已将WithEvents
放入ThisWorkbook模块。