This code check if an IBan account is valid or not
Code:
Debug.Print IBAN_Validator("BE72000000001616")
Code:
Option Explicit
Private Const IbanCountryLengths As String = "AL28AD24AT20AZ28BH22BE16BA20BR29BG22CR21HR21CY28CZ24DK18DO28EE20FO18" & _
"FI18FR27GE22DE22GI23GR27GL18GT28HU28IS26IE22IL23IT27KZ20KW30LV21LB28" & _
"LI21LT20LU20MK19MT31MR27MU30MC27MD24ME22NL18NO15PK24PS29PL28PT25RO24" & _
"SM27SA24RS22SK24SI19ES24SE24CH21TN24TR26AE23GB22VG24QA29"
Public Function IBAN_Validator(psIBAN As String) As Boolean
' #VBIDEUtils#************************************************************
' * Author :
' * Web Site :
' * E-Mail :
' * Date : 12/22/2014
' * Time : 14:30
' * Module Name : IBAN_Module
' * Module Filename : IBAN.bas
' * Procedure Name : IBAN_Validator
' * Purpose :
' * Parameters :
' * psIBAN As String
' * Purpose :
' **********************************************************************
' * Comments :
' *
' *
' * Example :
' *
' * See Also :
' *
' * History :
' *
' *
' **********************************************************************
Dim sIBAN As String
Dim nI As Integer
sIBAN = UCase(psIBAN)
' *** Remove spaces
sIBAN = Replace(sIBAN, " ", "")
' *** Check if psIBAN contains only uppercase characters and numbers
For nI = 1 To Len(sIBAN)
If Not ((Asc(Mid$(sIBAN, nI, 1)) <= Asc("9") And Asc(Mid$(sIBAN, nI, 1)) >= Asc("0")) Or _
(Asc(Mid$(sIBAN, nI, 1)) <= Asc("Z") And Asc(Mid$(sIBAN, nI, 1)) >= Asc("A"))) Then
IBAN_Validator = False
Exit Function
End If
Next
' *** Check if length of psIBAN equals expected length for country
If Not IBAN_ValidatorCountryLength(Left$(sIBAN, 2), Len(sIBAN)) Then
IBAN_Validator = False
Exit Function
End If
' *** Rearrange
sIBAN = Right$(sIBAN, Len(sIBAN) - 4) & Left$(sIBAN, 4)
' *** Replace characters
For nI = 0 To 25
sIBAN = Replace(sIBAN, Chr(nI + Asc("A")), nI + 10)
Next
' *** Check remainder
IBAN_Validator = Mod97(sIBAN) = 1
End Function
Private Function IBAN_ValidatorCountryLength(sCountryCode As String, nIBANLen As Integer) As Boolean
' #VBIDEUtils#************************************************************
' * Author :
' * Web Site :
' * E-Mail :
' * Date : 12/22/2014
' * Time : 14:30
' * Module Name : IBAN_Module
' * Module Filename : IBAN.bas
' * Procedure Name : IBAN_ValidatorCountryLength
' * Purpose :
' * Parameters :
' * sCountryCode As String
' * nIBANLen As Integer
' * Purpose :
' **********************************************************************
' * Comments :
' *
' *
' * Example :
' *
' * See Also :
' *
' * History :
' *
' *
' **********************************************************************
Dim i As Integer
For i = 0 To Len(IbanCountryLengths) / 4 - 1
If Mid$(IbanCountryLengths, i * 4 + 1, 2) = sCountryCode And CInt(Mid$(IbanCountryLengths, i * 4 + 3, 2)) = nIBANLen Then
IBAN_ValidatorCountryLength = True
Exit Function
End If
Next i
IBAN_ValidatorCountryLength = False
End Function
Private Function Mod97(Num As String) As Integer
' #VBIDEUtils#************************************************************
' * Author :
' * Web Site :
' * E-Mail :
' * Date : 12/22/2014
' * Time : 14:30
' * Module Name : IBAN_Module
' * Module Filename : IBAN.bas
' * Procedure Name : Mod97
' * Purpose :
' * Parameters :
' * Num As String
' * Purpose :
' **********************************************************************
' * Comments :
' *
' *
' * Example :
' *
' * See Also :
' *
' * History :
' *
' *
' **********************************************************************
Dim nTmp As Long
Dim sTmp As String
Do While Val(Num) >= 97
If Len(Num) > 5 Then
sTmp = Left$(Num, 5)
Num = Right$(Num, Len(Num) - 5)
Else
sTmp = Num
Num = ""
End If
nTmp = CLng(sTmp)
nTmp = nTmp Mod 97
sTmp = CStr(nTmp)
Num = sTmp & Num
Loop
Mod97 = CInt(Num)
End Function