VBA 中的文本框用户窗体控件(特别是 _BeforeUpdate)的集合

Collection of textbox userform controls (specifically _BeforeUpdate) in VBA

提问人:Lucas 提问时间:11/17/2023 更新时间:11/17/2023 访问量:31

问:

我已经阅读了有关此主题的类似问题的多个答案,但我找不到任何针对我的具体问题的解释。我想创建一个影响用户窗体中所有文本框的事件处理程序(我有 80 个文本框)。以下代码来自 Overstack 帮助,适用于 _Change() 控件,但不适用于 _BeforeUpdate(或 _AfterUpdate、_Exit 等)

ClassModule 中的以下代码:

Private WithEvents txtbox As MSForms.TextBox

Public Property Set TextBox(ByVal t As MSForms.TextBox)
    Set txtbox = t
End Property

Private Sub txtbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox "Worked in Exit"
    Cancel = True
End Sub

Private Sub txtbox_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox "it worked in BeforeUpdate " & Me.Name
    Cancel = True
End Sub

UserForm 中的代码:

Private myEventHandlers As Collection

Private Sub UserForm_Activate()
    Dim txtbox As mytextbox

    Set myEventHandlers = New Collection

    Dim c As Control
    For Each c In Me.Controls

        If c.Name Like "TextBox*" Then
            Set txtbox = New mytextbox

            Set txtbox.TextBox = c

            myEventHandlers.Add txtbox

        End If
    Next c

End Sub
Excel VBA

评论

1赞 Siddharth Rout 11/17/2023
stackoverflow.com/questions/23995931/......
0赞 Tim Williams 11/17/2023
stackoverflow.com/questions/65202856/......有一个(有效的?)解决方案

答: 暂无答案