This is from an XL VBA project I once did:
Class:
Public WithEvents TxtBx As MSForms.TextBox
Private Sub TxtBx_Change()
' -----------------------------------------------------------------------------
' This is the Class Module that is assigned to each textbox on the
' userform. It will trigger when each textbox is changed and as long
' as certain logic is True.
' -----------------------------------------------------------------------------
If Not Application.EnableEvents Or ClassTriggerEnabled = False Then Exit Sub
' We need to lock the Close Date textbox if there is no value present
' to stop the user from creating a new one. A Close Date can only be
' entered via a Follow-Up.
' If TxtBx.Name = "tbDateClosed" And ClntType = "Edit" Then
' ' If the textbox is blank and the data on the "Data" worksheet is
not,
' ' then the user has blanked the textbox himself, presumably to
enter
' ' a new Close Date, thus we will not lock the textbox.
' If TxtBx.Value = "" And
Worksheets("Data").Range("dDateClosed").Offset(frmDataEntry.sbFU.Value +
1).Value = "" Then
' TxtBx.Locked = True
' TxtBx.BackStyle = fmBackStyleTransparent
' Else
' TxtBx.Locked = False
' TxtBx.BackStyle = fmBackStyleOpaque
' End If
' End If
' If Mode = 1 Then Debug.Print "The control '" & TxtBx.Name & "' is being
changed."
' Extract the "format" property from the Tag property
' of the textbox control.
varTag = CheckTag(CtrlTag:=TxtBx.Tag, Fld:="Fmt")
' If Mode = 1 Then Debug.Print " Type => " & varTag
' Stop the routine from re-triggering when it changes
' a textbox's value.
Application.EnableEvents = False
' Format the textbox according to its designated format.
Select Case varTag
Case "Curr"
TxtBx = Format(TxtBx.Value, "Currency")
Case "Dt"
TxtBx = Format(TxtBx.Value, "mm/dd/yyyy")
Case "Ph"
TxtBx = Format(TxtBx.Value, "(###) ###-####")
Case "Num"
TxtBx = Format(TxtBx.Value, "#")
End Select
' Re-enable events.
Application.EnableEvents = True
End Sub
Then in your main code:
Main:
Dim Coll As Collection
Dim Tbx As CTxtBx
.....
.....
'
' -----------------------------------------------------------------------------
' This routine "groups" all textboxes into one collection
' so that one custom Class Module procedure can be used
' to format each textbox according to its format.
' -----------------------------------------------------------------------------
'
Sub SetupClassEventTxtBox()
Dim Ctrl As MSForms.Control
If ClntType = "ViewOnly" Then
cmbOK.Enabled = False
End If
Set Coll = New Collection
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
Set Tbx = New CTxtBx
Set Tbx.TxtBx = Ctrl
Coll.Add Tbx
End If
Next Ctrl
End Sub
This at least should get you going. As you can see, the class checks the tag
of the textbox to determine it's data format and the Select....Case then
formats the data accordingly. Hope this helps.