检查唯一编号

我需要检查JMBG(在我的国家是独一无二的公民身份)。 它有13个数字,由以下代码计算。 该函数返回我的JMBG是错误的。 也许在代码的某处我做了错误的计算。

这是一个例子。 现实生活中的JMBG是0805988212987,这个函数返回错误的月份。

Function Check_JMBG(JMBG As String) As String ' Function returns message with notification of JMBG validation ' JMBG has 13 numbers and can be treated like this when checking it DD.MM.GGG.OO.BBB.K ' Details of JMBG (unique citizenship number in my country, is 13 by the way): 'DD - day of birth 'MM - manth of birth 'GGG - last 3 numbers of year of birth, starting from (1)899. year 'OO - municipality birth code 'BBB - serial number of birth person. Man from 001-499, woman from 501-999 'K - control number, modulo 11 Dim size As Integer, sum As Integer Dim number(1 To 13) As Integer Dim day As Integer, manth As Integer, year As String size = Len(JMBG) day = Int(Left(JMBG, 2)) manth = Int(Mid$(JMBG, 3, 2)) year = Mid$(JMBG, 5, 3) ' Size check If (size <> 13) Then Check_JMBG = "ERR: size of JMBG is not 13!" End If 'Date check If day < 1 Then Check_JMBG = "ERR: date entered is wrong!" Exit Function End If 'Manth check and date inside manth Select Case manth Case 1, 3, 5, 7, 8, 10, 12 If day > 31 Then Check_JMBG = "ERR: date number is wrong!" Exit Function End If Case 4, 6, 9, 11 If day > 30 Then Check_JMBG = "ERR: data number is wrong!" Exit Function End If Case 2 If ((year Mod 4 = 0) And day > 29) Or _ ((year Mod 4 <> 0) And day > 28) Then Check_JMBG = "ERR: date number is wrong!" Exit Function End If Case Else Check_JMBG = "ERR: manth number is wrong!" Exit Function End Select 'Check year: from 1899 till today If (year > Right(str(Year(Now)), 3)) And (year < "899") Then Check_JMBG = "ERR: year number is wrong!" Exit Function End If 'Control number check For i = 1 To 13 number(i) = Int(Mid$(JMBG, i, 1)) Next i sum = number(13) + number(1) * 7 + number(2) * 6 sum = sum + number(3) * 5 + number(4) * 4 sum = sum + number(5) * 3 + number(6) * 2 sum = sum + number(7) * 7 + number(8) * 6 sum = sum + number(9) * 5 + number(10) * 4 sum = sum + number(11) * 3 + number(12) * 2 If (sum Mod 11) <> 0 Then Check_JMBG = "ERR: wrong control number!" Else Check_JMBG = "JMBG is correct" End If End Function 

我运行了你的代码,它在这个月里工作正常。 我得到一个编译错误,因为你使用相同的名称为variables年和VBA函数 – 一旦我用strYearreplace它也可以。

看看下面的重构代码,看看这是否解决了你的问题:

函数Check_JMBG(JMBG As String)As String
    如果(Len(JMBG)<> 13)那么
         Check_JMBG =“ERR:JMBG的长度不是13!”
    否则如果不是数字(JMBG)那么
         Check_JMBG =“ERR:JMBG包含非数字字符”
    否则如果不是fctBlnCheckDate(JMBG)那么
         Check_JMBG =“错误:input错误的date!”
    否则如果fctBlnCheckSum(JMBG)那么
         Check_JMBG =“错误:校验和错误!”
    其他
         Check_JMBG =“JMBG正确”
    万一
结束function

私人函数fctBlnCheckDate(JMBG作为string)作为布尔值
     Dim intDay As Integer,intMonth As Integer,intYear As Integer
     Dim datCheck As Date

     intDay = Int(Left(JMBG,2))
     intMonth = Int(Mid $(JMBG,3,2))
     intYear = Int(Mid $(JMBG,5,3))+ 1000

     datCheck = DateSerial(intYear,intMonth,intDay)

     fctBlnCheckDate = _
         (year(datCheck)= intYear)和_
         (月(datCheck)= intMonth)和_
         (day(datCheck)= intDay)

结束function

私人函数fctBlnCheckSum(JMBG作为string)作为布尔值
     Dim intCheckSum As Integer,i As Integer

    对于我= 1到13
         intCheckSum = intCheckSum + Int(Mid $(JMBG,i,1))*(IIf(i <7,8,14) -  i)
    下一个 
     fctBlnCheckSum =(intCheckSum Mod 11)<> 0 
结束function

你的函数期待一个string,并且你正在传递一个数字。 由于大小检查后不能退出该function,因此您不会收集错误。

=Check_JMBG(805988212987)给出错误manth是错误的

=Check_JMBG("0805988212987")给出的消息JMBG是正确的

格式化有所作为

注意单元格A1左上方的绿色三angular形…这意味着我已经通过在数字前面包括一个' (单引号)来input数字作为文本。

你可以稍微改变你的testing来覆盖缺失的零。 – 在获取大小之后,在提取date,月份和年份之前,请input以下代码:

 If (size <> 13) Then 'add leading zeros JMBG = String(13 - size, "0") & JMBG End If 

完整代码:

 Function Check_JMBG(JMBG As String) As String ' Function returns message with notification of JMBG validation ' JMBG has 13 numbers and can be treated like this when checking it DD.MM.GGG.OO.BBB.K ' Details of JMBG (unique citizenship number in my country, is 13 by the way): 'DD - day of birth 'MM - manth of birth 'GGG - last 3 numbers of strYear of brith, starting from (1)899. strYear 'OO - municipality birth code 'BBB - serial number of birth person. Man from 001-499, woman from 501-999 'K - control number, modulo 11 Dim size As Integer, sum As Integer Dim number(1 To 13) As Integer Dim day As Integer, manth As Integer, strYear As String size = Len(JMBG) ' Size check If (size <> 13) Then JMBG = String(13 - size, "0") & JMBG End If day = Int(Left(JMBG, 2)) manth = Int(Mid$(JMBG, 3, 2)) strYear = Mid$(JMBG, 5, 3) 'Date check If day < 1 Then Check_JMBG = "ERR: date entered is wrong!" Exit Function End If 'Manth check and date inside manth Select Case manth Case 1, 3, 5, 7, 8, 10, 12 If day > 31 Then Check_JMBG = "ERR: date number is wrong!" Exit Function End If Case 4, 6, 9, 11 If day > 30 Then Check_JMBG = "ERR: data number is wrong!" Exit Function End If Case 2 If ((strYear Mod 4 = 0) And day > 29) Or _ ((strYear Mod 4 <> 0) And day > 28) Then Check_JMBG = "ERR: date number is wrong!" Exit Function End If Case Else Check_JMBG = "ERR: month number is wrong!" Exit Function End Select 'Check strYear: from 1899 till today If (strYear > Right(str(Year(Now)), 3)) And (strYear < "899") Then Check_JMBG = "ERR: strYear number is wrong!" Exit Function End If 'Control number check For i = 1 To 13 number(i) = Int(Mid$(JMBG, i, 1)) Next i sum = number(13) + number(1) * 7 + number(2) * 6 sum = sum + number(3) * 5 + number(4) * 4 sum = sum + number(5) * 3 + number(6) * 2 sum = sum + number(7) * 7 + number(8) * 6 sum = sum + number(9) * 5 + number(10) * 4 sum = sum + number(11) * 3 + number(12) * 2 If (sum Mod 11) <> 0 Then Check_JMBG = "ERR: wrong control number!" Else Check_JMBG = "JMBG is correct" End If End Function