在 VBA 用户窗体中,退出归档时触发哪个事件 [英] In a VBA Userform, which event is triggered when exiting a filed

查看:25
本文介绍了在 VBA 用户窗体中,退出归档时触发哪个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 10(64 位)平台上使用 Microsoft Office Professional Plus(64 位).我有一个子例程,当我更改名为 MyDate 的用户表单字段时会处理该子例程.它被称为 Private Sub MyDate_AfterUpdate().它是表单上的第二个字段.只要 MyDate 字段的内容被编辑,它就可以正常工作.但是,如果用户不需要更新 MyDate 字段的内容,因为他们接受该字段的默认值,只需按 Tab 键越过第二个字段,我仍然喜欢子例程被执行.当我只是通过 Tab 键浏览该字段而不必编辑内容时,我可以使用什么事件来激活代码?感谢您查看此内容.

I'm using Microsoft Office Professional Plus (64 bit) on a Windows 10 (64 bit) platform. I have a subroutine that is processed when I make a change to a Userform field called MyDate. It's called Private Sub MyDate_AfterUpdate(). It's the second field on a form. It works fine as long as the contents of the MyDate field are edited. However, if the user doesn't need to update the contents of the MyDate field because they accept the default of the field and just presses the tab key past that second field, I'd still like the subroutine to be executed. What event can I use to activate code when I simply tab through the field and don't necessarily edit the contents? Thanks for looking at this.

推荐答案

如果您查看代码窗格的顶部,您会注意到两个下拉菜单.左边的包含您可以在该类中实现的所有接口和事件提供程序(UserForm 是一个类).

If you look at the top of the code panes, you'll notice two dropdowns. The left one contains all interfaces and event providers you can implement in that class (a UserForm is a class).

从下拉列表中选择您的 MyDate 控件;右侧下拉列表现在列出了您可以为此 MyDate 控件处理的每个事件:

Select your MyDate control from that dropdown; the right-side dropdown is now listing every event you could handle for this MyDate control:

在这种特殊情况下,Exit 事件似乎是一个不错的选择:

In this particular case, the Exit event seems a good candidate:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'make Cancel.Value = True to prevent exiting the control.
    '...ideally... make that conditional...
End Sub

通过始终使用这些下拉菜单让 VBE 为您生成事件处理程序(而不是从内存中键入它们),您可以避免出错……并且错误的事件处理程序签名可以从字面上做任何事情全部,如果幸运的话编译错误,或者如果你不太幸运的话,会出现奇怪且难以诊断的行为.

By consistently using these dropdowns to let the VBE generate event handler procedures for you (instead of typing them up from memory), you avoid getting it wrong... and getting an event handler signature wrong can do anything from literally nothing at all, to compile errors if you're lucky, or weird and hard-to-diagnose behavior if you're less lucky.

这篇关于在 VBA 用户窗体中,退出归档时触发哪个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆