I've made my first attempt at making a number checksum creator/validation. After going round in circles, I've given up 
Can some kind person please tell me where I've gone wrong with the below?
'140731
Option Explicit
Option Private Module
Private Const mbytcMod As Byte = 11
Public Sub TestMod11Checksum()
Dim avarWeights() As Variant
Dim strRegNo As String
Dim bytCheckDigit As Byte
Dim blnIsValidChecksum As Boolean
avarWeights = Array(1, 4, 3, 7, 5, 8, 6, 9, 10)
Debug.Print "Create random 8 digit string"
strRegNo = Int(Rnd * 100000000#)
Debug.Print "Random no. = " & strRegNo
Debug.Print "Attempt calc of checksum digit for random no."
bytCheckDigit = fn_bytCalcChecksumDigitMod11(strRegNo, avarWeights)
Debug.Print "Check digit = " & bytCheckDigit
Debug.Print "To test checksum validation works, we will add the check digit to the end of the 8 digit no. then run this through the function"
strRegNo = strRegNo & bytCheckDigit
Debug.Print "Random no. is now " & strRegNo
blnIsValidChecksum = fn_blnValidChecksumMod11(strRegNo, avarWeights)
Debug.Print "The checksum validation returned: " & blnIsValidChecksum
End Sub
Public Function fn_bytCalcChecksumDigitMod11(ByVal strChecksum As String, ByVal avarWeightFactors As Variant) As Byte
'provide the full weighting array - i.e. include the weight for the checksum digit
Dim bytPosition As Byte
Dim intSum As Integer
Dim bytCurrent As Byte
Dim blnValid As Boolean
Dim bytWeight As Byte
Dim bytArrOptFix As Byte
Dim bytRemainder As Byte
Dim intCheckDigit As Integer
If Not IsNumeric(strChecksum) Then
Exit Function
End If
If Not IsArray(avarWeightFactors) Then
Exit Function
End If
If Not Len(strChecksum) = UBound(avarWeightFactors) - LBound(avarWeightFactors) Then
Exit Function
End If
'auto handle base0 and base1 arrays
bytArrOptFix = 1 - LBound(avarWeightFactors)
'Multiply each digit by its weighting factor
For bytPosition = 1 To Len(strChecksum) - 1
'get the value of the current digit
bytCurrent = CInt(Mid(strChecksum, bytPosition, 1))
'get the value of the current weight factor
bytWeight = avarWeightFactors(bytPosition - bytArrOptFix)
'add the multiplied value to the total sum
intSum = intSum + (bytWeight * bytCurrent)
Next bytPosition
'I'm dubious about the next few lines of code
Select Case intSum
Case Is < mbytcMod
intCheckDigit = mbytcMod - intSum
Case Is = mbytcMod
intCheckDigit = 0
Case Is > mbytcMod
bytRemainder = intSum Mod mbytcMod
If bytRemainder = 0 Then
intCheckDigit = 0
ElseIf bytRemainder > 0 Then
intCheckDigit = ((bytRemainder + 1) * mbytcMod) - intSum
Else
Debug.Assert False
'shouldn't reach here?
End If
End Select
fn_bytCalcChecksumDigitMod11 = intCheckDigit
End Function
Public Function fn_blnValidChecksumMod11(ByVal strChecksum As String, ByVal avarWeightFactors As Variant) As Boolean
Dim bytPosition As Byte
Dim intSum As Integer
Dim bytCurrent As Byte
Dim blnValid As Boolean
Dim bytWeight As Byte
Dim bytArrOptFix As Byte
If Not IsNumeric(strChecksum) Then
Exit Function
End If
If Not IsArray(avarWeightFactors) Then
Exit Function
End If
If Not Len(strChecksum) = UBound(avarWeightFactors) - LBound(avarWeightFactors) + 1 Then
Exit Function
End If
'auto handle base0 and base1 arrays
bytArrOptFix = 1 - LBound(avarWeightFactors)
'Multiply each digit by its weighting factor
For bytPosition = 1 To Len(strChecksum)
'get the value of the current digit
bytCurrent = CInt(Mid(strChecksum, bytPosition, 1))
'get the value of the current weight factor
bytWeight = avarWeightFactors(bytPosition - bytArrOptFix)
'add the multiplied value to the total sum
intSum = intSum + (bytWeight * bytCurrent)
Next bytPosition
'If total is exactly divisible by mod factor then checksum is valid
blnValid = (intSum Mod mbytcMod = 0)
fn_blnValidChecksumMod11 = blnValid
End Function
Bookmarks