Skip digits when running

Asked

Viewed 44 times

1

I have a problem, I took right here a script to enter 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

However I need the function to skip the first two digits (the DDD) and check the number from the third digit, someone knows how to do this?

1 answer

0

You used this answer: /a/262274/75104, then for more information about the code, refer to the answer given.

Regex

A new Regex is required, in which the demo of Regex101 can be seen 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.