Fonction VBA permettant de vérifier la validité d’un GENCOD (EAN13)

 Utilisable en VBA (excel, word…)

Public Function Gencod(codeGen) As Boolean
'Cette fonction renvoie true si le GENCOD n'est pas valide

Dim wcod As String
Dim x As Integer
Dim totPair, totImpair, totGen As Long
Dim cle, w10 As Integer
Gencod = False
wcod = Len(Trim(codeGen))
If wcod <> 13 Or IsNumeric(Trim(codeGen)) = False Then
Gencod = True
Exit Function
End If
For x = 1 To 12
If x Mod 2 = 0 Then
'pair
totPair = totPair + CInt(Mid(Trim(codeGen), x, 1))
Else
'impair
totImpair = totImpair + CInt(Mid(Trim(codeGen), x, 1))
End If
Next x
totGen = (totPair * 3) + totImpair
w10 = Fix(totGen / 10) + 1
cle = (w10 * 10) - totGen
if cle >= 10 then cle = 0
If cle <> CInt(Mid(codeGen, 13, 1)) Then
Gencod = True
Exit Function
End If
End Function

Print Friendly, PDF & Email