18 de ago. de 2011

Como fazer verificação de CPF e PIS - PASEP no Excel

Como fazer verificação de CPF e PIS - PASEP no Excel

CPF:

Abra o editor do Visual Basic (alt+F11) e adicione um novo módulo (menu inserir - módulo).

Copie e cole o código entre os traços:
------------------------------
unction Checa_cpf(TRIPA As String) As Boolean

Dim wcpf As String
Dim WCPF2 As String
Dim wi As Integer
Dim wpedaco As String
Dim wtot As Integer

wcpf = ""

For wi = 1 To Len(TRIPA)
wpedaco = Mid(TRIPA, wi, 1)
If wpedaco >= "0" And wpedaco <= "9" Then wcpf = wcpf & wpedaco
Next wi
If Len(wcpf) <> 11 Then
Checa_cpf = False
End If

For wi = 1 To 9
wtot = wtot + (11 - wi) * Val(Mid(wcpf, wi, 1))
Next wi

wdig1 = wtot Mod 11
If wdig1 < 2 Then
wdig1 = 0
Else
wdig1 = 11 - wdig1
End If
WCPF2 = Left(wcpf, 9) & wdig1

wtot = 0
For wi = 1 To 10
wtot = wtot + (12 - wi) * Val(Mid(WCPF2, wi, 1))
Next
wdig2 = wtot Mod 11
If wdig2 < 2 Then
wdig2 = 0
Else
wdig2 = 11 - wdig2
End If

Checa_cpf = (Right(wcpf, 2) = wdig1 & wdig2)

End Function

----------------------------------
Depois você verifica uma celular que contenha o CPF através da formula: =Checa_cpf(A1). Ele retorna Verdadeiro ou Falso.

Para verificar ou validar o PIS - PASEP:

Abra o editor do Visual Basic (alt+F11) e adicione um novo módulo (menu inserir - módulo).

Copie e cole o código entre os traços:
-------------------------------------

Public Function PISPASEP(numero As String)
Dim ftap As String
Dim total As String
Dim i As Integer
Dim resto As Integer

If Val(numero) = 0 Or Len(numero) <> 11 Then
  PISPASEP = False
  Exit Function
End If

ftap = "3298765432"
total = 0

For i = 1 To 10
  total = total + Val(Mid(numero, i, 1)) * Val(Mid(ftap, i, 1))
Next i

resto = Int(total Mod 11)

If resto <> 0 Then
  resto = 11 - resto
End If

If resto <> Val(Mid(numero, 11, 1)) Then
  PISPASEP = False
  Exit Function
End If

PISPASEP = True

End Function

----------------------------------

Depois você verifica uma celular que contenha o PIS através da formula: =PISPASEP(A1). Ele retorna Verdadeiro ou Falso.