Formula & Sub to format phone numbers working by itself but not in conjunction with other code.

llane23

New Member
Joined
Jan 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
Hello,

I'm getting a #NAME? error when I try to insert this code into a much longer Macro, but it works perfectly when I just do it on its own. For reference, there are messy numbers in column V that need to be formatted into (XXX) XXX-XXXX.


Function PhoneFormat(Phone As String) As String
Dim i As Integer, sFormat As String, sCur As String, sTrunc As String, n As Integer
sTrunc = Phone
For i = 1 To Len(sTrunc)
If IsNumeric(Mid(sTrunc, i, 1)) Then n = n + 1
Next i
If n > 10 Then sTrunc = Right(sTrunc, Len(sTrunc) - 1)

For i = 1 To Len(sTrunc)
sCur = Mid(sTrunc, i, 1)
If IsNumeric(sCur) Then
Select Case Len(sFormat)
Case 0: sFormat = "(" & sCur
Case 3: sFormat = sFormat & sCur & ") "
Case 8: sFormat = sFormat & sCur & "-"
Case Else: sFormat = sFormat & sCur
End Select
End If
Next i
PhoneFormat = sFormat
End Function

Sub PhoneColumn()
Dim r As Long, c As Integer, i As Long, iLast As Long
Range("V1").Select
r = ActiveCell.Row
c = ActiveCell.Column
iLast = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
ActiveCell.Offset(0, 1).EntireColumn.Insert
Range(Cells(2, c + 1), Cells(iLast, c + 1)).FormulaR1C1 = "=PhoneFormat(RC[-1])"
Range("W:W").EntireColumn.Copy
Range("V1").PasteSpecial (xlPasteValues)
Range("W:W").EntireColumn.Delete
Range("V1").Value = "Phone Number"

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
That is a lot of code for that.

Here is a shorter PhoneFormat function:

VBA Code:
Function PhoneFormat(p As String) As String
Dim i As Long, t As String, s As String
For i = 1 To Len(p)
    t = Mid(p, i, 1)
    If IsNumeric(t) And t <> "" Then s = s & t
Next
PhoneFormat = Format(s, "(###) ###-####")
End Function


If you are going to run a macro anyway, why not just convert the values in place?

VBA Code:
Sub PhoneColumn()
Dim c As Range
For Each c In Range("V2:V" & Range("V" & Rows.Count).End(xlUp).Row)
    c = PhoneFormat(c.Text)
Next
End Sub
 
Upvote 0
I think the whole copy/paste scenario happened because I was trying to troubleshoot why it wasn't working within the context of the whole macro. The function works properly on its own and so does the macro that houses the formula. But when I get it into the worksheet I need it to work on, it just gives me that error. I really just need the "1" to be removed from the beginning of it if the number represents the country code.
 
Upvote 0
VBA Code:
Function PhoneFormat(p As String) As String
Dim i As Long, t As String, s As String
For i = 1 - (Left(p, 1) = "1") To Len(p)
    t = Mid(p, i, 1)
    If IsNumeric(t) And t <> "" Then s = s & t
Next
PhoneFormat = Format(s, "(###) ###-####")
End Function



Sub PhoneColumn()
Dim c As Range
For Each c In Range("V2:V" & Range("V" & Rows.Count).End(xlUp).Row)
    c = PhoneFormat(c.Text)
Next
End Sub
 
Upvote 0
My code is shorter and I think it does what you need (I adjusted it for the potential leading 1), but your function should be in a standard module.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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