Validar el CIF/NIF
6.01.2008
‘03/04/2005 ver 1.0b
‘Valida el NIF o el CIF, averiguando cúal de los dos es.
Function Validar_NIF_CIF(Codigo As String) As Boolean
Dim strDigito, strLetra, respuesta As String
Dim X As Integer
‘Comprueba que sólo el primer y/o último dígito son/es una letra.
For X = 2 To (Len(Codigo) – 1)
strDigito = Mid(Codigo, X, 1)
If InStr(”ABCDEFGHIJKLMNÑOPQRSTUVWXYZ”, strDigito) <> 0 Then
respuesta = MsgBox(”El código introducido no se corresponde ni con un CIF ni con un NIF, verifíquelo.”, vbOKOnly + vbCritical, “Ironda”)
Validar_NIF_CIF = False
Exit Function
End If
Next X
‘Comprueba si se trata de un CIF
strLetra = UCase(Left(Codigo, 1))
If strLetra >= “A” And strLetra <= “Z” Then
If Validar_CIF(Codigo) = True Then
Validar_NIF_CIF = True
Exit Function
Else
respuesta = MsgBox(”El CIF introducido es incorrecto, verifíquelo.”, vbOKOnly + vbCritical, “Ironda”)
Validar_NIF_CIF = False
Exit Function
End If
End If
‘Comprueba si se trata de un NIF
strLetra = UCase(Right(Codigo, 1))
If strLetra >= “A” And strLetra <= “Z” Then
If strLetra = Letra_NIF(Val(Codigo)) Then
Validar_NIF_CIF = True
Exit Function
Else
respuesta = MsgBox(”El NIF introducido es incorrecto, verifíquelo.”, vbOKOnly + vbCritical, “Ironda”)
Validar_NIF_CIF = False
Exit Function
End If
End If
‘Si no tiene letra lo trata como a una NIF, y se la añade
respuesta = MsgBox(”El código introducido no se corresponde ni con un NIF ni con un CIF, verifíquelo.”, vbOKOnly + vbCritical, “Ironda”)
Validar_NIF_CIF = False
End Function
‘03/04/2005 ver 1.0b
‘Calcula la letra del NIF
Public Function Letra_NIF(DNI As Long) As String
Const constLetra As String = “TRWAGMYFPDXBNJZSQVHLCKE”
Letra_NIF = Mid(constLetra, (DNI Mod 23) + 1, 1)
End Function
‘03/04/2005 ver 1.0b
‘Comprueba que el CIF introducido es correcto
Public Function Validar_CIF(sCIF As String) As Boolean
Dim strPri, strUlt, strResto, strImpares(1 To 4) As String
Dim i, iSuma As Integer
strPri = UCase(Left(sCIF, 1)) ‘Guarda letra
strUlt = Right(sCIF, 1) ‘Guarda dígito de control
strResto = Mid(sCIF, 2, 7) ‘Guarda cuerpo CIF, usado para cálculo
‘Suma de posiciones pares
iSuma = CInt(Mid(strResto, 2, 1)) + CInt(Mid(strResto, 4, 1)) + CInt(Mid(strResto, 6, 1))
‘Multiplico posiciones impares por dos, y sumo cifras resultantes
strImpares(1) = Format(2 * CInt(Mid(strResto, 1, 1)), “00″)
strImpares(2) = Format(2 * CInt(Mid(strResto, 3, 1)), “00″)
strImpares(3) = Format(2 * CInt(Mid(strResto, 5, 1)), “00″)
strImpares(4) = Format(2 * CInt(Mid(strResto, 7, 1)), “00″)
For i = 1 To 4
iSuma = iSuma + (CInt(Left(strImpares(i), 1)) + CInt(Right(strImpares(i), 1)))
Next i
‘Obtengo la posición de unidades, distinguiendo si es un organismo oficial o extranjero
‘Si no es organismo oficial o extranjero:
’strTipo(0) = “NIF – Número de Identificación Fiscal.”
’strTipo(1) = “A – Sociedad Anónima.”
’strTipo(2) = “B – Sociedad de responsabilidad limitada.”
’strTipo(3) = “C – Sociedad colectiva.”
’strTipo(4) = “D – Sociedad comanditaria.”
’strTipo(5) = “E – Comunidad de bienes.”
’strTipo(6) = “F – Sociedad cooperativa.”
’strTipo(7) = “G – Asociación.”
’strTipo(8) = “H – Comunidad de propietarios.”
’strTipo(9) = “K – Formato antiguo.”
’strTipo(10) = “L – Formato antiguo.”
’strTipo(11) = “M – Formato antiguo.”
’strTipo(12) = “N – Formato antiguo.”
’strTipo(13) = “P – Corporación local.”
’strTipo(14) = “Q – Organismo autónomo.”
’strTipo(15) = “S – Organo de la administración.”
’strTipo(16) = “X – NIE – Número Identificador de Extranjeros.”
’strTipo(17) = “ERROR”
If Not strPri = “X” And Not strPri = “P” And Not strPri = “S” And Not strPri = “Q” Then
If strUlt = Right(CStr(10 – CInt(Right(CStr(iSuma), 1))), 1) Then
Validar_CIF = True
Else
Validar_CIF = False
End If
Else ‘Si es organismo oficial o extranjeto busco la letra de control:
If strUlt = Chr(64 + (10 – CInt(Right(CStr(iSuma), 1)))) Then
Validar_CIF = True
Else
Validar_CIF = False
End If
End If
End Function