Public Function GetCheckdigit(VarScanlineData As Variant) As Integer
Dim Weights As New Collection
Dim WeightedDigits As New Collection
Dim I As Integer
Dim J As Integer
Dim NumberOfDigits As Integer
Dim Digit As Integer
Dim WeightedDigit As Integer
Dim WeightedSum As Integer
Dim GrandSum As Integer
Dim GrandSumMod10
Dim strDigit As String
Weights.Add (7)
Weights.Add (5)
Weights.Add (3)
Weights.Add (2)
'Get Number of
NumberOfDigits = Len(CStr(VarScanlineData))
' Not sure here if it is always 7 digits. It is a lot simpler if this is the case
' But I will assume the weight pattern just repeats itself so the scanline data can be any length
For I = 1 To NumberOfDigits
Digit = CInt(Mid(CStr(VarScanlineData), I, 1))
J = J + 1
WeightedDigit = Digit * Weights(J)
Debug.Print "Digit:" & Digit & " Weighted Digit: " & WeightedDigit
If J = Weights.Count Then J = 0
'Add the weighted digits to a collection
WeightedDigits.Add (WeightedDigit)
Next I
For I = 1 To WeightedDigits.Count
WeightedDigit = WeightedDigits(I)
strDigit = CStr(WeightedDigit)
For J = 1 To Len(strDigit)
WeightedSum = WeightedSum + CInt(Mid(strDigit, J, 1))
Next J
Debug.Print "Weighted Sum " & WeightedSum
GrandSum = GrandSum + WeightedSum
WeightedSum = 0
Next I
Debug.Print "GrandSum " & GrandSum
'Mod 10 of Grand Sum
GrandSumMod10 = GrandSum Mod 10
Debug.Print GrandSum & " Mod 10: " & GrandSumMod10
'Get Check digit
If GrandSumMod10 = 0 Then
GetCheckdigit = 0
Else
GetCheckdigit = 10 - GrandSumMod10
End If
Debug.Print "Check digit: " & GetCheckdigit
End Function