在Excel中validationIBAN

有没有准备好的Excel表格来validationIBAN? 我将进入IBAN,它将显示有效或无效。

我search了一些Adds In,并find了这个

但我不知道如何打开它。 谁能帮忙?

这很简单,只需使用下面的function。

'' Validate IBAN Public Function VALIDATEIBAN(ByVal IBAN As string) As Boolean On Error GoTo Catch Dim objRegExp As Object Dim blnIsValidIBAN As Boolean Set objRegExp = CreateObject("vbscript.regexp") objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.Pattern = "^[a-zA-Z]{2}\d{2}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{4}|CZ\d{22}$" blnIsValidIBAN = objRegExp.Test(IBAN) VALIDATEIBAN = blnIsValidIBAN Exit Function Catch: VALIDATEIBAN = False MsgBox "Module: " & MODULE_NAME & " - VALIDATEIBAN function" & vbCrLf & vbCrLf _ & "Error#: " & Err.Number & vbCrLf & vbCrLf & Err.Description End Function 

如何使用:

 Copy the code. In Excel press Alt + F11 to enter the VBE. Press Ctrl + R to show the Project Explorer. Insert -> Module. Paste code. Save and Exit VBE. 

运行该function:

现在在Excel中有一个用户定义的函数,就像内置的SUM,AVG函数一样。 假设您想在单元格A1中validationIBAN,只需在任何单元格中写入=VALIDATEIBAN(A1). 它将返回TRUE或FALSE。

此外,它将同时适用于:

 ES65 0800 0000 1920 0014 5399 

 ES6508000000192000145399 

但不是:

 ES65-0800-0000-1920-0014-5399 

那么这个格式化的问题就解决了,执行了“97检查”:

 Public Function VALIDATEIBAN(ByVal IBAN As String) As String ' Created by : Koen Rijnsent (www.castoro.nl) ' Inspired by : Chris Fannin (AbbydonKrafts) ' Inspired by : bonsvr (http://stackoverflow.com/users/872583/bonsvr) On Error GoTo CatchError Dim objRegExp As Object Dim IBANformat As Boolean Dim IBANNR As String Dim ReplaceChr As String Dim ReplaceBy As String 'Check format IBAN = UCase(IBAN) Set objRegExp = CreateObject("vbscript.regexp") objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.Pattern = "[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}" IBANformat = objRegExp.Test(IBAN) 'Validity of country code will not be checked! If IBANformat = False Then VALIDATEIBAN = "FORMAT NOT RECOGNIZED" Else 'Flip first 4 characters to the back IBANNR = Right(IBAN, Len(IBAN) - 4) & Left(IBAN, 4) 'Replace letters by the right numbers For Nr = 10 To 35 ReplaceChr = Chr(Nr + 55) ReplaceBy = Trim(Str(Nr)) IBANNR = Replace(IBANNR, ReplaceChr, ReplaceBy) Next Nr 'Loop through the IBAN, as it is too long to calculate at one go CurrPart = "" Answer = "" For CurrDigit = 1 To Len(IBANNR) CurrPart = CurrPart & Mid$(IBANNR, CurrDigit, 1) CurrNumber = CLng(CurrPart) 'If the number can be divided If 97 <= CurrNumber Then LeftOver = CurrNumber Mod 97 WorkValue = (CurrNumber - LeftOver) / 97 Answer = Answer & CStr(WorkValue) CurrPart = CStr(LeftOver) Else 'If no division occurred, add a trailing zero If Len(Answer) > 0 Then Answer = Answer & "0" 'Exception for the last number If CurrDigit = Len(IBANNR) Then LeftOver = CurrNumber Mod 97 Else End If Else End If End If Next If LeftOver = 1 Then VALIDATEIBAN = "IBAN OK" Else VALIDATEIBAN = "97 CHECK FAILED" End If End If Exit Function CatchError: VALIDATEIBAN = "ERROR: " & Err.Description MsgBox "Module: " & MODULE_NAME & " - VALIDATEIBAN function" & vbCrLf & vbCrLf _ & "Error#: " & Err.Number & vbCrLf & vbCrLf & Err.Description End Function 

我发现从bonsvr有用的答案,谢谢。 从我的代码阅读来看,这似乎是特定于CZ帐户的范围。

由于我主要处理爱尔兰,英国和德国的IBAN代码,所以我开发了这个正则expression式,用objRegExp.Pattern = …代替行。

 objRegExp.Pattern = "^[GB|IE]{2}\d{2}[a-zA-Z]{4}\d{14}|[DE]\d{20}$" 

我希望这可以帮助别人,因为最初的代码帮助了我。 如果你想添加自己的国家,扩展以上。

注:我删除提供的空格( [ ] ),因为我正在testing的文本没有。 如果您希望每4个字符添加一次,这很容易实现,或者更简单一些,将上面的代码的第一行replace为:

 IBAN = Trim(Ucase(Replace(IBAN, " ", ""))) 

这将消除空间,削减在前面和后面的任何额外的空间,并转换为大写。 (修剪可能是多余的,但皮带和括号…)

正则expression式由[GB | IE](GB或IE)组成,允许使用ISO国家代码,后跟2位数校验和,4位数字库代码和14位数字,格式相同这两个国家的IBAN格式。 德国允许使用另一个“或”,然后是22位数字。 要添加另一个国家,只需将您的文本放在$符号之前,以|开头。

在这里find其他国家的格式 (维基百科)

 Option Compare Database Option Explicit ' http://en.wikipedia.org/wiki/International_Bank_Account_Number Private Const IbanCountryLengths As String = "AL28AD24AT20AZ28BH22BE16BA20BR29BG22CR21HR21CY28CZ24DK18DO28EE20FO18" & _ "FI18FR27GE22DE22GI23GR27GL18GT28HU28IS26IE22IL23IT27KZ20KW30LV21LB28" & _ "LI21LT20LU20MK19MT31MR27MU30MC27MD24ME22NL18NO15PK24PS29PL28PT25RO24" & _ "SM27SA24RS22SK24SI19ES24SE24CH21TN24TR26AE23GB22VG24QA29" Private Function ValidateIbanCountryLength(CountryCode As String, IbanLength As Integer) As Boolean Dim i As Integer For i = 0 To Len(IbanCountryLengths) / 4 - 1 If Mid(IbanCountryLengths, i * 4 + 1, 2) = CountryCode And _ CInt(Mid(IbanCountryLengths, i * 4 + 3, 2)) = IbanLength Then ValidateIbanCountryLength = True Exit Function End If Next i ValidateIbanCountryLength = False End Function Private Function Mod97(Num As String) As Integer Dim lngTemp As Long Dim strTemp As String Do While Val(Num) >= 97 If Len(Num) > 5 Then strTemp = Left(Num, 5) Num = Right(Num, Len(Num) - 5) Else strTemp = Num Num = "" End If lngTemp = CLng(strTemp) lngTemp = lngTemp Mod 97 strTemp = CStr(lngTemp) Num = strTemp & Num Loop Mod97 = CInt(Num) End Function Public Function ValidateIban(IBAN As String) As Boolean Dim strIban As String Dim i As Integer strIban = UCase(IBAN) ' Remove spaces strIban = Replace(strIban, " ", "") ' Check if IBAN contains only uppercase characters and numbers For i = 1 To Len(strIban) If Not ((Asc(Mid(strIban, i, 1)) <= Asc("9") And Asc(Mid(strIban, i, 1)) >= Asc("0")) Or _ (Asc(Mid(strIban, i, 1)) <= Asc("Z") And Asc(Mid(strIban, i, 1)) >= Asc("A"))) Then ValidateIban = False Exit Function End If Next i ' Check if length of IBAN equals expected length for country If Not ValidateIbanCountryLength(Left(strIban, 2), Len(strIban)) Then ValidateIban = False Exit Function End If ' Rearrange strIban = Right(strIban, Len(strIban) - 4) & Left(strIban, 4) ' Replace characters For i = 0 To 25 strIban = Replace(strIban, Chr(i + Asc("A")), i + 10) Next i ' Check remainder ValidateIban = Mod97(strIban) = 1 End Function 

来源: http : //www.aswinvanwoudenberg.com/2013/07/18/vba-iban-validator/