我正在学习 vba 的事件处理。这是我的第一次尝试。
'in sheet Object
Dim WithEvents engine As MCengine
Private Sub engine_OnEachTrial(ByVal i As Integer)
progressBar = Application.Floor(i * 30 / engine.numOfPaths, 0.001)
With ActiveSheet.Range("E10").Characters(start:=0, Length:=progressBar).Font
.Name = "Calibri"
.FontStyle = "Bold"
.Size = 11
.ColorIndex = 16
End With
End Sub
Private Sub Worksheet_Activate()
Dim btn As Button
Dim rng As Range
Set engine = New MCengine
With ActiveSheet
Set rng = .Range("E9")
Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height)
With btn
.Caption = "Run MC simulation"
.OnAction = "runMC"
End With
End With
End Sub
Sub runMC()
engine.process = 1
engine.numOfPaths = 30
engine.start
End Sub
上面代码的问题是onAction注册的子过程不能被调用,因为看起来runMC必须在Module中。我无法将这些代码移动到模块,因为Dim WithEvents engine As MCengine
需要在工作表对象中声明。所以我被困在中间。我应该使用其他按钮回调方法吗?
这是否意味着我不能在模块中使用带有事件的类?任何人都可以启发我吗?