VBA 文本框更改触发宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25449443/
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-12 04:21:21  来源:igfitidea点击:

VBA textbox changes trigger macro

excel-vbavbaexcel

提问by Tom

I am trying to monitor a sheet which has several text boxes on it.

我正在尝试监控一个上面有几个文本框的工作表。

The idea behind it is the user enters text into these boxes and then you can click the submit button to send it all to a sql database.

它背后的想法是用户在这些框中输入文本,然后您可以单击提交按钮将其全部发送到 sql 数据库。

Now I want to have it so if the user has made changes, and they go to leave the sheet a macro is triggered to tell them that they haven't saved it.

现在我想要它,所以如果用户进行了更改,并且他们离开工作表,则会触发一个宏来告诉他们他们还没有保存它。

I've already got this triggering on the deactivate worksheet event, but I was wondering if I can monitor all of the textboxes on the sheet (oleobjects) under one change event

我已经在停用工作表事件上触发了此事件,但我想知道是否可以在一个更改事件下监视工作表(oleobjects)上的所有文本框

I am already assuming this isn't possible with just one but was hoping.

我已经假设这不可能只用一个,但希望如此。

Thanks in advance

提前致谢

Tom

汤姆

回答by Ross McConeghy

One way to do this would be to write a separate subroutine that is called within the Change event of all the Textboxes. Keep in mind though that this will be raised every time the Textboxes change at all- every keystroke.

一种方法是编写一个单独的子例程,在所有文本框的 Change 事件中调用该子例程。请记住,每次文本框更改时都会引发此问题 - 每次击键。

In each TextBox_Change Event:

在每个 TextBox_Change 事件中:

Private Sub TextBox1_Change()
    TextChanged TextBox1
End Sub

New subroutine:

新子程序:

Sub TextChanged(fromTextBox As TextBox)
    'validation code goes here
End Sub