'Download PDF version here
'Language: VBA
'----------------------------------------------------------------------------------------------------
'An ISIN number verification function,
featuring a single for-loop algorithm.
'In practice, structural compliance of
the input has been performed by earlier verification functions.
'Down this stage, the input is assumed
to be properly formatted according to the ISIN guideline.
'And only checksum verification is left
to be carried out.
'----------------------------------------------------------------------------------------------------
'Author: Leap Ahead Consulting -
www.leapahead.cm
'----------------------------------------------------------------------------------------------------
Public Function IsValidIsinCheckSum (ByVal ISINCode As String) As Boolean
Dim Char As
String
Dim LenCode As
Integer
Dim k As
Integer
Dim ComputedCheckSum
As Integer
Dim CheckSum
As Integer
Dim Coeff
As Integer
Dim AsciiCode
As Integer
Dim CharValue
As Integer
Dim LeadDigit
As Integer
Dim TrailingDigits
As Integer
Dim LocalSum
As Integer
ComputedCheckSum
= 0
'Mandatory setting
Coeff =
1
'Mandatory setting
IsValidIsinCheckSum = False
'Mandatory setting
LenCode = Len(ISINCode)
For k = (LenCode -1) To 1 Step -1
Char = Mid(ISINCode, k, 1)
'Get current character, from right to left
AsciiCode = Asc(Char)
'Get char ASCII value, then apply correct offset
value
If ((AsciiCode >= 48) And (AsciiCode <= 57)) Then
CharValue = (AsciiCode -
48)
'Handle digits [0-9]
ElseIf ((AsciiCode >= 65) And (AsciiCode <= 90)) Then
CharValue = (AsciiCode -
55)
'Handle upper case [A-Z]
ElseIf ((AsciiCode >= 97) And (AsciiCode <= 122)) Then
CharValue = (AsciiCode -
87)
'Support for low case [a-z], just in case!
Else
CharValue = 0
'Got an invalid-set character!! Early termination
is an option, or let it run and fail gracefully.
End If
Coeff = IIf((Coeff = 1), 2, 1)
'Updating multiplicative factor (1, or 2) on the
fly
TrailingDigits = (CharValue Mod 10) * Coeff
'Trailing digit(s) - Might result in a two-digit
number, to be further split
LeadDigit = (Int(CharValue / 10))
'Get the integer value of the division result
If (LeadDigit > 0) Then
Coeff = IIf((Coeff = 1), 2, 1)
'Updating multiplicative factor (1, or 2) on the
fly
LeadDigit = LeadDigit * Coeff
'Non-zero leading one-digit number
End If
LocalSum = LeadDigit + (Int(TrailingDigits / 10)) + (TrailingDigits
Mod 10)
'Up to three non-zero digit sum
ComputedCheckSum = ComputedCheckSum
+ LocalSum
'Incremental sum
Next k
If (ComputedCheckSum
> 0) Then
CheckSum = Int(Mid(ISINCode,
LenCode, 1))
'Integer value of last character (digit) in input
code
ComputedCheckSum = (10 - (ComputedCheckSum Mod 10)) Mod 10
'Final computed checksum
If (CheckSum = ComputedCheckSum) Then IsValidIsinCheckSum = True
'Validity check
End If
End Function