Formatting a textbox to accept specific characters

Nour

New Member
Joined
Jun 16, 2021
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
Hello house. Pls I need assistance on this issue....
I have a phone number textbox which is formatted to accept on numerical values of upto 11 characters
Problem:- I want the phone number format to always start with either 070, 080, or 090 followed by the rest of the 8 digits that can be any value.
Request:- pls how do I make these first 3 characters of the textbox to accept only these digits. Bear in mind the 1st and 3rd characters must only be 0 while the 2nd varies from 7 to 9. Thanks.
 
Try:
VBA Code:
Private Sub TextBox1_Change()
' the first, second and third char:  (0-1, 7-9, 0-2)
' length 11 char
Dim x As Long
Dim tx As String
tx = "[0-1][7-9][0-2]########"
    With TextBox1
        x = Len(.Text)
        Select Case x
            ' uncomment ': Beep  if you want a beep sound
            Case 1 To 3
                If Not .Text Like Left(tx, x * 5) Then .Text = Left(.Text, x - 1)  ': Beep
            Case Is > 3
                If Not .Text Like Left(tx, x + 12) Then .Text = Left(.Text, x - 1) ': Beep
        End Select
    End With
    
End Sub


To understand what the pattern "[0-1][7-9][0-2]########" means, please read this article:

Let me know if you need more explanation.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try:
VBA Code:
Private Sub TextBox1_Change()
' the first, second and third char:  (0-1, 7-9, 0-2)
' length 11 char
Dim x As Long
Dim tx As String
tx = "[0-1][7-9][0-2]########"
    With TextBox1
        x = Len(.Text)
        Select Case x
            ' uncomment ': Beep  if you want a beep sound
            Case 1 To 3
                If Not .Text Like Left(tx, x * 5) Then .Text = Left(.Text, x - 1)  ': Beep
            Case Is > 3
                If Not .Text Like Left(tx, x + 12) Then .Text = Left(.Text, x - 1) ': Beep
        End Select
    End With
   
End Sub


To understand what the pattern "[0-1][7-9][0-2]########" means, please read this article:

Let me know if you need more explanation.
Thank you very much for the prompt assistance Akuini. I really can't express enough my appreciation.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,640
Messages
6,125,972
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top