订阅VBA中的事件? [英] Subscribe to events in VBA?
问题描述
我是否正确理解您只能使用VBA编辑器的控件名称+事件名称组合框来订阅事件?像其他语言(如C#)一样,没有combobox.change + = eventhandler语法可用吗?
Did I understood correctly that you can't subscribe to an event other than using the VBA Editor's control name + event name comboboxes? There is no combobox.change+=eventhandler syntax available as in other languages like C#, is there?
推荐答案
正确.事件处理通过VB6/VBA中的命名约定完成.
名称可以是控件itlsef的名称,也可以是声明为 WithEvents
的变量.通过为该变量分配其他引用,您可以开始从该新对象接收事件.这可以看作是动态订阅.
Correct. Event handling is done via naming convention in VB6/VBA.
The name can be the name of the control itlsef, or it can be a variable declared WithEvents
. By assigning this variable a different reference, you start receiving events from that new object. This can be seen as dynamical subscribing.
但是,某些环境可以提供其他手段.例如,在Excel中,某些可放置对象具有隐藏属性,例如 Button.OnAction
.您可以为该属性分配一个宏名称(作为字符串),然后在按下按钮时调用它.
However, certain environments can provide additional means. For example, in Excel some placeable objects have hidden properties, such as Button.OnAction
. You can assign a name of a macro to this property (as a string), and it will be called on button press.
这篇关于订阅VBA中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!