Formatting a textbox to accept specific characters

Nour

New Member
Joined
Jun 16, 2021
Messages
20
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.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,981
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Nour

New Member
Joined
Jun 16, 2021
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,981
Office Version
  1. 365
Platform
  1. Windows
You're welcome, glad to help & thanks for the feedback.:)
 

Forum statistics

Threads
1,175,957
Messages
5,900,534
Members
434,835
Latest member
cmenconi

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
Top