Skip digits when running

1

I have a problem, I got a script here to insert the ninth digit if the phone starts with 6,7,8,9 ..

Function ValidarCelular(Myrange As Range) As String
    On Error GoTo ErrHandler:
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "^[6|7|8|9](?:\d{7}|\d{3}-\d{4})$"

    If strPattern <> "" Then
        strInput = Trim(Myrange.Value)
        strReplace = "9" & strInput

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With
            If regEx.test(strInput) Then
                ValidarCelular = regEx.Replace(strInput, strReplace)
            Else
                ValidarCelular = Myrange.Value
            End If
    End If
Exit Function
ErrHandler:
    ' Tratamento de Erro
    ValidarCelular= CVErr(xlErrNA)
    On Error GoTo 0
End Function

But I need the function to skip the first two digits (DDD) and check the number from the third digit, does anyone know how to do this?

    
asked by anonymous 04.05.2018 / 16:29

1 answer

0

You used this answer: link , then for more information about the code, please refer to the answer given.

Regex

A new Regex is required, where the Regex101 demo can be viewed here.

Regex: ^\(*\d{2}\)*\s*[6|7|8|9](?:\d{7}|\d{3}\s*-\s*\d{4})$

Code

So the code becomes:

Function ValidarCelular(Myrange As Range) As String
    On Error GoTo ErrHandler:
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "^\(*\d{2}\)*\s*[6|7|8|9](?:\d{7}|\d{3}\s*-\s*\d{4})$"

    If strPattern <> "" Then
        strInput = Trim(Myrange.Value)
        strReplace = "9" & strInput

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With
            If regEx.test(strInput) Then
                ValidarCelular = regEx.Replace(strInput, strReplace)
            Else
                ValidarCelular = Myrange.Value
            End If
    End If
Exit Function
ErrHandler:
    ' Tratamento de Erro
    ValidarCelular= CVErr(xlErrNA)
    On Error GoTo 0
End Function
    
04.05.2018 / 19:02