New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Allowing only one phone number input format.

Macros, VBA, Excel Automation, etc.

Allowing only one phone number input format.

Postby FrancisM » Thu Dec 03, 2015 8:11 am

There are multiple people using this worksheet to, and they all have their own way of inputting phone numbers. I am trying to allow only one type of formatting. Here is the code. [Sub PhoneNumberFormat()
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 And Len(Trim(Target.Value)) > 0 Then
Target.Value = onlyNumbers(Target.Value)
Target.NumberFormat = "(000) 000-0000"
End If
End Sub

Private Function onlyNumbers(myVal As Variant) As Variant
Dim l As Integer
For l = 1 To Len(myVal)
If IsNumeric(Mid(myVal, l, 1)) Then onlyNumbers = onlyNumbers & Mid(myVal, l, 1)
Next l
End Function]
I have attached the file because I am having some hang up issues.
Thanks for your assistance.
  • 0

You do not have the required permissions to view the files attached to this post.
FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby NoSparks » Thu Dec 03, 2015 10:10 am

Try this on the sheet module only, remove the standard module you have
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
' the cell being monitored
    If Target.Address <> "$I$30" Or Target.Value = "" Then Exit Sub
' unprotect sheet
    ActiveSheet.Unprotect
' prevent this procedure from calling itself
    Application.EnableEvents = False
' change what's in the cell
    Target.Value = onlyNumbers(Target.Value)
    Target.NumberFormat = "(000) 000-0000"
' re-enable events
    Application.EnableEvents = True
' re-protect sheet
    ActiveSheet.Protect
End Sub


Private Function onlyNumbers(myVal As Variant) As Variant
    Dim i   As Integer
    For i = 1 To Len(myVal)
        If IsNumeric(Mid(myVal, i, 1)) Then onlyNumbers = onlyNumbers & Mid(myVal, i, 1)
    Next i
End Function

  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby FrancisM » Thu Dec 03, 2015 10:56 am

NoSparks . Thanks it work perfectly.
  • 0

FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby NoSparks » Thu Dec 03, 2015 11:04 am

You're welcome, glad I could help and thanks for reporting back.
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby FrancisM » Tue Dec 08, 2015 10:15 am

NoSparks. I just learned that I I click on row 28 :Address., I get a run time error 13 type mismatch.
below is the code & it hangs on If Target.Column = 9 And Len(Trim(Target.Value)) > 0 Then

[Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 9 And Len(Trim(Target.Value)) > 0 Then
Target.Value = onlyNumbers(Target.Value)
Target.NumberFormat = "(000) 000-0000"
End If
End Sub]
Any ideas?
if you use the sample file I sent earlier it happens there also.
  • 0

FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby FrancisM » Tue Dec 08, 2015 1:29 pm

Code: Select all
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 9 And Len(Trim(Target.Value)) > 0 Then
 Target.Value = onlyNumbers(Target.Value)
 Target.NumberFormat = "(000) 000-0000"
End If
End Sub
  • 0

FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby NoSparks » Tue Dec 08, 2015 2:44 pm

Any ideas?

Yup, MERGED CELLS.
  • 0

Last edited by NoSparks on Wed Dec 09, 2015 9:39 am, edited 1 time in total.
NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby FrancisM » Tue Dec 08, 2015 3:05 pm

How stupid of me. You are correct. Merged cells. Thank You!!!!!
  • 0

FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby FrancisM » Thu Dec 17, 2015 2:03 pm

Question. I tried to use the same formula for social security numbers changing field $I$30 to $H$21, and (000) 000-0000 to 000-00-0000. When I run it there is no change. Any ideas.
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
' the cell being monitored
    If Target.Address <> "$H$21" Or Target.Value = "" Then Exit Sub
' unprotect sheet
    ActiveSheet.Unprotect
' prevent this procedure from calling itself
    Application.EnableEvents = False
' change what's in the cell
    Target.Value = onlyNumbers(Target.Value)
    Target.NumberFormat = "000-000-0000"
' re-enable events
    Application.EnableEvents = True
' re-protect sheet
    ActiveSheet.Protect
End Sub
  • 0

FrancisM
Regular
 
Posts: 44
Joined: Jan 30, 2013
Reputation: 0
Excel Version: 2010

Re: Allowing only one phone number input format.

Postby NoSparks » Thu Dec 17, 2015 2:55 pm

Your text indicates the format as 000-00-0000
the code you've posted has it as 000-000-0000
you'll need to adjust if necessary.
Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
' the cells being monitor
    If Target.Address <> "$I$30" And Target.Address <> "$H$21" Then Exit Sub
    If Target.Value = "" Then Exit Sub
' unprotect sheet
    ActiveSheet.Unprotect
' prevent this procedure from calling itself
    Application.EnableEvents = False
' change what's in cell if I30
    If Target.Address = "$I$30" Then
        Target.Value = onlyNumbers(Target.Value)
        Target.NumberFormat = "(000) 000-0000"
    End If
'change what's in cell if H21
    If Target.Address = "$H$21" Then
        Target.Value = onlyNumbers(Target.Value)
        Target.NumberFormat = "000-000-0000"
    End If
' re-enable events
    Application.EnableEvents = True
' re-protect sheet
    ActiveSheet.Protect
End Sub
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010

Next

Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 225 guests