The VBE's Project Explorer is making it rather hard to tell/guess what specific procedure you intend to invoke (Rubberduck's Code Explorer drilling down to procedure-level nodes would have helped here! Note: I'm one of the admins on this open-source project), but anyway it looks like you need to invoke a procedure located in the Sheet1
document module.
Say your procedure looks like this:
Public Sub DoSomething()
' do stuff
End Sub
Then from anywhere in your project, you can do this:
Sheet1.DoSomething
Which isn't any different than when you do this:
MsgBox "hello, world"
That's invoking a public/global-scope function located in a standard library function, whose fully-qualified name would be:
VBA.Interaction.MsgBox "hello, world"
Now, if your procedure looks like this:
Private Sub Worksheet_Change()
' do stuff
End Sub
那么你有一个问题:它是Private
,它是一个事件处理程序。事件处理程序可以Public
像上面一样创建和调用,但是更好的做法是将主体拉入一个单独的过程,如下所示:
Private Sub Worksheet_Change()
DoSomething
End Sub
Public Sub DoSomething()
' do stuff
End Sub
这样你就不用处理事件处理程序了。工作表事件处理程序旨在由 Excel 调用,而不是由用户代码调用;这同样适用于例如CommandButton1_Click
处理程序:它们旨在Private
由 MSForms 库调用,而不是由用户代码调用。养成在事件处理程序之外实现逻辑的习惯(并让它们调用逻辑),是一个非常好的习惯,可以尽早养成。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句