vba 将事件侦听器添加到 Excel 文本框(失去焦点)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13566287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:35:46  来源:igfitidea点击:

Add event listener to Excel Textbox (lose focus)

exceleventsvbaexcel-vbatextbox

提问by Ole Henrik Skogstr?m

I need to fire an sub or a command when a user is done using a text box in Excel.

当用户使用 Excel 中的文本框完成时,我需要触发一个子项或命令。

I have tried using the AfterUpdate() event and the LoseFocus() event like this:

我曾尝试使用 AfterUpdate() 事件和 LoseFocus() 事件,如下所示:

Public Sub Kommentar_AfterUpdate()

MsgBox ("Hurray")

End Sub

The text box is named Kommentarand is inside the sheet Radio. Also, where is the code supposed to be written? i have tried placing it in code sheet for the Radio sheet, and in a separate module.

文本框已命名Kommentar并位于工作表内Radio。另外,代码应该写在哪里?我曾尝试将它放在 Radio 表的代码表中,以及一个单独的模块中。

Any tip, hint or answer is appreciated!

任何提示,提示或答案表示赞赏!

回答by InContext

For embedded ActiveX Excel control - add the following in the sheet mobile in VBA. TextBox1 is the name of the control:

对于嵌入式 ActiveX Excel 控件 - 在 VBA 中的移动表中添加以下内容。TextBox1 是控件的名称:

Private Sub TextBox1_LostFocus()


End Sub

For userform - use the following where Textbox1 is the name of your textbox:

对于用户表单 - 使用以下内容,其中 Textbox1 是您的文本框的名称:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)


End Sub

You can set the Cancel bool to True if you do not want to lose focus from the textbox. In addition the code is placed within the UserForm >> Right Click >> View Code.

如果您不想失去文本框的焦点,您可以将 Cancel bool 设置为 True。此外,代码放置在用户窗体 >> 右键单击​​ >> 查看代码中。