Option Explicit
Public WithEvents tbGroup As MSForms.TextBox 'используем коллекцию в качестве класса
Dim bu As Boolean, s$ 'mask 8(###) ###-##-##
Private Sub tbGroup_Change()
If bu = True Then bu = False: Exit Sub
'MsgBox tbGroup.Name
s = tbGroup.Text
Select Case Len(s)
Case 0, 1
s = "8("
Case 3, 4, 5
If Not IsNumeric(Right(s, 1)) Then s = Left(s, Len(s) - 1) Else Exit Sub
Case 6
If Not IsNumeric(Right(s, 1)) Then
s = Left(s, Len(s) - 1)
Else
s = Left(s, Len(s) - 1) & ") " & Right(s, 1)
End If
Case 8, 9, 10, 12, 13, 15, 16
If Not IsNumeric(Right(s, 1)) Then s = Left(s, Len(s) - 1) Else Exit Sub
Case 11, 14
If Not IsNumeric(Right(s, 1)) Then
s = Left(s, Len(s) - 1)
Else
s = Left(s, Len(s) - 1) & "-" & Right(s, 1)
End If
Case Is > 16: s = Left(s, Len(s) - 1)
End Select
bu = True: tbGroup.Value = s
End Sub
|