Buona sera a tutti.
Da molto tempo ho utilizzato la seguente funzione ( trovata in rete ) che mi effettua il controllo dei codici fiscali dei dipendenti inseriti in una determinata cella ( A2).
La eseguo in questo modo:
in B2 =VerificaCodFis(A2)
Option Explicit
Public Function VerificaCodFis(ByVal sCF As String) As Variant
Dim oRE As RegExp
Dim oMatch As MatchCollection
Dim sPatt As String
Dim j As Integer
Dim nHash As Integer
Dim vRet As Variant
Dim aOddTable As Variant
Dim aEvenTable As Variant
Dim aCarryTable As Variant
'Tabella dei caratteri dispari
aOddTable = Array(Array("0", 1), Array("1", 0), Array("2", 5), Array("3", 7), _
Array("4", 9), Array("5", 13), Array("6", 15), Array("7", 17), \_
Array("8", 19), Array("9", 21), Array("A", 1), Array("B", 0), \_
Array("C", 5), Array("D", 7), Array("E", 9), Array("F", 13), \_
Array("G", 15), Array("H", 17), Array("I", 19), Array("J", 21), \_
Array("K", 2), Array("L", 4), Array("M", 18), Array("N", 20), \_
Array("O", 11), Array("P", 3), Array("Q", 6), Array("R", 8), \_
Array("S", 12), Array("T", 14), Array("U", 16), Array("V", 10), \_
Array("W", 22), Array("X", 25), Array("Y", 24), Array("Z", 23))
'Tabella dei caratteri pari
aEvenTable = Array(Array("0", 0), Array("1", 1), Array("2", 2), Array("3", 3), _
Array("4", 4), Array("5", 5), Array("6", 6), Array("7", 7), \_
Array("8", 8), Array("9", 9), Array("A", 0), Array("B", 1), \_
Array("C", 2), Array("D", 3), Array("E", 4), Array("F", 5), \_
Array("G", 6), Array("H", 7), Array("I", 8), Array("J", 9), \_
Array("K", 10), Array("L", 11), Array("M", 12), Array("N", 13), \_
Array("O", 14), Array("P", 15), Array("Q", 16), Array("R", 17), \_
Array("S", 18), Array("T", 19), Array("U", 20), Array("V", 21), \_
Array("W", 22), Array("X", 23), Array("Y", 24), Array("Z", 25))
'Tabella dei verifica del carattere di controllo
aCarryTable = Array(Array(0, "A"), Array(1, "B"), Array(2, "C"), Array(3, "D"), _
Array(4, "E"), Array(5, "F"), Array(6, "G"), Array(7, "H"), \_
Array(8, "I"), Array(9, "J"), Array(10, "K"), Array(11, "L"), \_
Array(12, "M"), Array(13, "N"), Array(14, "O"), Array(15, "P"), \_
Array(16, "Q"), Array(17, "R"), Array(18, "S"), Array(19, "T"), \_
Array(20, "U"), Array(21, "V"), Array(22, "W"), Array(23, "X"), \_
Array(24, "Y"), Array(25, "Z"))
'controllo correttezza sintattica tramite RegEx
vRet = False
Set oRE = New RegExp 'CreateObject("vbscript.regexp")
sPatt = "^[A-Z]{6}[0-9]{2}[ABCDEHLMPRST][0-9]{2}A-Z[A-Z]$"
With oRE
.Global = True
.ignorecase = True
oRE.pattern = sPatt
If oRE.Test(sCF) Then
'controllo correttezza semantica tramite verifica 16 carattere:
'il carattere della tabella aCarryTable, corrispondente al resto della somma
'dei valori dei 15 caratteri diviso 26, deve essere uguale al 16 carattere del c.f.
For j = 1 To 15
If Application.IsEven(j) Then 'pari
nHash = nHash + Application.VLookup(VBA.Mid(sCF, j, 1), aEvenTable, 2, 0)
Else
nHash = nHash + Application.VLookup(VBA.Mid(sCF, j, 1), aOddTable, 2, 0)
End If
Next
nHash = nHash Mod 26
If Application.VLookup(nHash, aCarryTable, 2, 0) = VBA.Right(sCF, 1) Then
vRet = True
End If
End If
End With
Set oRE = Nothing
Set oMatch = Nothing
VerificaCodFis = vRet
End Function
Ho notato che sulla base di 60000 codici fiscali dei dipendenti controllati, solo quelli sotto riportati ( ho riportato solo gli ultimi 5 caratteri per privacy e per poter spiegare come sono strutturati) sono stati segnalati dalla funzione, come FALSO.
Premetto che i codici fiscali segnalati come FALSO li ho controllati tramite L'Agenzia delle Entrare e sono validi.
| H5L1D |
| A6S2A |
| 8P9Q |
| G2T3L |
| E7V1C |
| F8P9Q |
| F8P9Y |
| H5L1C |
| H5L1G |
| D7L8D |
| H2N4B |
| H5L1T |
| F8P9T |
| F8P9S |
| H5L1A |
| H5L1G |
| H5L1L |
| H5L1P |
| F8P9I |
| C3R1O |
Questi codici fiscali, differenti dai soliti come per es :CNTNCL84A22E541A non terminano gli ultimi 4 caratteri con 3 cifre e 1 lettera, bensì con 1 cifra, 1 lettera, 1 cifra e 1 lettera finale.
Ciò posto, chiedo a voi professionisti se sia possibile nella routine gestire anche questi nuovi codici fiscali impostati nel modo in cui li ho specificati.
Spero di essere stato chiaro.
Ringrazio chi mi aiuta in questo.
Ciao, Nicola.