Public Function IsValidCPF(ByVal strCPF As String) As Boolean

Option Explicit

 

Public Function IsValidCPF(ByVal strCPF As String) As Boolean

Dim intDigito As Integer

Dim intIndex As Integer

Dim intModulo As Integer

Dim intSoma1 As Integer

Dim intSoma2 As Integer

Dim intDigitoVerificador1 As Integer

Dim intDigitoVerificador2 As Integer

 

intSoma1 = 0

intSoma2 = 0

 

For intIndex = 1 To 9

intDigito = CInt(Mid$(strCPF, intIndex, 1))

intSoma1 = intSoma1 + ((11 - intIndex) * intDigito)

intSoma2 = intSoma2 + ((12 - intIndex) * intDigito)

Next

 

intModulo = intSoma1 Mod 11

If intModulo < 2 Then

intDigitoVerificador1 = 0

Else

intDigitoVerificador1 = 11 - intModulo

End If

 

intSoma2 = intSoma2 + (2 * intDigitoVerificador1)

 

intModulo = intSoma2 Mod 11

If intModulo < 2 Then

intDigitoVerificador2 = 0

Else

intDigitoVerificador2 = 11 - intModulo

End If

 

IsValidCPF = ( _

(Mid$(strCPF, 10, 1) = CStr(intDigitoVerificador1)) And _

(Mid$(strCPF, 11, 1) = CStr(intDigitoVerificador2)) _

)

End Function

 

No Comments