segunda-feira, 9 de agosto de 2010

ValidaCPF

Function ValidaCPF(cpf As String) As Boolean



' Verificando se o CPF é verdadeiro

carcterescpf = Len(CPF)
For i=1 To carcterescpf
caracter = Mid(cpf,i,1)
If Not(caracter = "0" Or caracter = "1" Or caracter = "2" Or caracter = "3" Or caracter = "4" Or caracter = "5"_
Or caracter = "6" Or caracter = "7" Or caracter = "8" Or caracter = "9") Then
ValidaCPF = True
Exit Function
End If
Next

If carcterescpf <11 Then zeros = "" For i = 1 To (11-carcterescpf ) zeros = "0" & zeros Next cpf = zeros & cpf End If 'Cálculo do 1º Dígito verificador dv1=0 elem1=Mid(cpf,1,1) res1=Cdbl(elem1)*10 elem2=Mid(cpf,2,1) res2=Cdbl(elem2)*9 elem3=Mid(cpf,3,1) res3=Cdbl(elem3)*8 elem4=Mid(cpf,4,1) res4=Cdbl(elem4)*7 elem5=Mid(cpf,5,1) res5=Cdbl(elem5)*6 elem6=Mid(cpf,6,1) res6=Cdbl(elem6)*5 elem7=Mid(cpf,7,1) res7=Cdbl(elem7)*4 elem8=Mid(cpf,8,1) res8=Cdbl(elem8)*3 elem9=Mid(cpf,9,1) res9=Cdbl(elem9)*2 soma=res1+res2+res3+res4+res5+res6+res7+res8+res9 resto=soma Mod 11 If resto=0 Or resto=1Then dv1=0 Else dv1=11 - resto 'Cálculo do 2º Dígito Verificador dv2=0 elem1dv2=Mid(cpf,1,1) res1dv2=Cdbl(elem1dv2)*11 elem2dv2=Mid(cpf,2,1) res2dv2=Cdbl(elem2dv2)*10 elem3dv2=Mid(cpf,3,1) res3dv2=Cdbl(elem3dv2)*9 elem4dv2=Mid(cpf,4,1) res4dv2=Cdbl(elem4dv2)*8 elem5dv2=Mid(cpf,5,1) res5dv2=Cdbl(elem5dv2)*7 elem6dv2=Mid(cpf,6,1) res6dv2=Cdbl(elem6dv2)*6 elem7dv2=Mid(cpf,7,1) res7dv2=Cdbl(elem7dv2)*5 elem8dv2=Mid(cpf,8,1) res8dv2=Cdbl(elem8dv2)*4 elem9dv2=Mid(cpf,9,1) res9dv2=Cdbl(elem9dv2)*3 elem10=dv1 res10dv2=elem10 * 2 soma2=res1dv2+res2dv2+res3dv2+res4dv2+res5dv2+res6dv2+res7dv2+res8dv2+res9dv2+res10dv2 resto2=soma2 Mod 11 If resto2=0 Or resto2=1 Then dv2=0 Else dv2=11 - resto2 testadv1=0 testadv2=0 cpfdv1=Cdbl(Mid(cpf,10,1)) If cpfdv1>11 Or testadv1=1 Or testadv2=1 Then
ValidaCPF = True
Exit Function
End If


End Function

Nenhum comentário:

Postar um comentário