View Single Post
 
Old 05-03-2018, 10:21 PM
NoSparks NoSparks is offline Windows 7 64bit Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

For what it's worth...
I'd use VBA and the Worksheet_Change event in the sheet module.

This should remove everything but the numbers that are typed into the cell.
Count how many numbers you're left with and format them.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Telnum As String
    
'limit to single cell
If Target.Count > 1 Then Exit Sub
'limit to in column B
If Target.Column = 2 And Target.Row > 1 Then
    'ok to deal with this
    Telnum = OnlyNums(Target.Value2)
    Select Case Len(Telnum)
        Case Is < 10
            'do nothing leave as is
            Telnum = Target.Value2
        Case Is = 10
            Telnum = "(" & Left(Telnum, 3) & ") " & Mid(Telnum, 4, 3) & "-" & Mid(Telnum, 7)
        Case Is > 10
            Telnum = "(" & Left(Telnum, 3) & ") " & Mid(Telnum, 4, 3) & "-" & Mid(Telnum, 7, 4) & " x" & Mid(Telnum, 11)
    End Select
    Target.NumberFormat = "@"
    Application.EnableEvents = False
    Target.Value = Telnum
    Application.EnableEvents = True
End If
    
End Sub


Function OnlyNums(sWord As String)
    Dim sChar As String
    Dim x As Integer
    Dim sTemp As String

    sTemp = ""
    For x = 1 To Len(sWord)
        sChar = Mid(sWord, x, 1)
        If Asc(sChar) >= 48 And _
          Asc(sChar) <= 57 Then
            sTemp = sTemp & sChar
        End If
    Next
    OnlyNums = Val(sTemp)
End Function
Reply With Quote