Binding an excel forms textbox exit event to a class module

Alex Baja 21 Reputation points
2025-08-21T13:39:47.0666667+00:00

I am having trouble binding a series of textboxe exit events on an excel form to a class module;

Here is My class code:

Public WithEvents myEBtn As MSForms.TextBox

Private Sub myEBtn_end(ByVal Cancel As MSForms.ReturnBoolean)

'MsgBox "hello"

myEBtn.Value = Format(myEBtn.Value, "#,##0")

End Sub

Here is My form initialization code"

Private MyETextBox(1 To 3) As New ExitBtn

Private Sub UserForm_Initialize()

Dim i As Long

Dim ctl As Control

ctl = Me.Controls("txt1")

ctl = Me.Controls("txt" & i)

For i = 1 To 3

Set MyETextBox(i).myEBtn = Me.Controls("txt" & i)

Next i

End Sub

can someone help me?

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. HansV 462K Reputation points MVP Volunteer Moderator
    2025-08-21T15:45:49.7466667+00:00

    As the link that I posted shows, you have to make the code jump through a lot of hoops to simulate the event in a class module. So I wouldn't use one. I'd create a common procedure in the userform module itself, or in a standard module if you want to apply it to multiple userforms.

    And I'd use the After Update event instead of the Exit event - we only need to format the value if the control's value has changed.

    Private Sub txt1_AfterUpdate()
        Call FormatMe(txt1)
    End Sub
    Private Sub txt2_AfterUpdate()
        Call FormatMe(txt2)
    End Sub
    Private Sub txt3_AfterUpdate()
        Call FormatMe(txt3)
    End Sub
    Private Sub FormatMe(ctl As MSForms.TextBox)
        If IsNumeric(ctl) Then
            ctl = Format(ctl, "#,##0")
        End If
    End Sub
    
    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. HansV 462K Reputation points MVP Volunteer Moderator
    2025-08-21T14:07:44.8133333+00:00

    I think you meant myEBtn_Exit instead of myEBtn_end. But unfortunately, a custom control does not have an Exit event, nor a BeforeUpdate or AfterUpdate event.

    Perhaps Controling colors of controls in userform will help, although it's quite old.

    0 comments No comments

  2. Alex Baja 21 Reputation points
    2025-08-21T15:21:23.5366667+00:00

    thanks. then if I have 20 controls on a form, how can I format them as "#,##0" through a class module

    0 comments No comments

  3. Alex Baja 21 Reputation points
    2025-08-21T15:54:39.5+00:00

    I understand that and what you have posted is common for each individual text box. I am trying to avoid 20 afterupdates in My code. If I was reseloved to that from the beginning, I would have done that, but there must be a way to bind that into 1 code snippet for all 20 textboxes through a class module.


  4. Alex Baja 21 Reputation points
    2025-08-21T18:27:45.69+00:00

    Thanks for your help. It was instructional. And as a result I found a way to accomplish this through the class module event.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.