我在想什么。
我有一个VBA宏Excel代码,用于检查唯一公民号码,并且返回是否正确。
但要启动它,我必须在启动Excel时调用Macro,按ALT + F11,而不是输入代码,然后输入代码,然后编译它,然后在Excel函数中输入我的函数Check_JMBG(adr),我可以在Excel工作表中调用它。
但是当我启动其他一些Excel文件时,我再次为此目的创建了宏。
现在我想知道我可以制作一个示例,当我启动其他Excel文件并立即调用它时,我怎么能让我的函数立即出现在那里?
调用函数示例:Check_JMBG(A1),在A1中输入为文本填写:2003969265121
以下是检查唯一公民号码的代码:
' Function for checking JMBG validation
Function Check_JMBG(JMBG As String) As String
If (Len(JMBG) <> 13) Then
Check_JMBG = "ERROR: Length of JMBG is not 13!"
ElseIf Not IsNumeric(JMBG) Then
Check_JMBG = "ERROR: JMBG contains non-numerical characters"
ElseIf Not fctBlnCheckDate(JMBG) Then
Check_JMBG = "ERROR: Wrong date entered!"
ElseIf fctBlnCheckSum(JMBG) Then
Check_JMBG = "ERROR: Wrong checksum!"
Else
Check_JMBG = "JMBG is correct"
End If
End Function
' Function to check JMBG day, month, year validation
Private Function fctBlnCheckDate(JMBG As String) As Boolean
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) And _
(Month(datCheck) = intMonth) And _
(Day(datCheck) = intDay)
End Function
' Function to check JMBG control number and control sum
Private Function fctBlnCheckSum(JMBG As String) As Boolean
Dim intCheckSum As Integer, i As Integer
For i = 1 To 13
intCheckSum = intCheckSum + Int(Mid$(JMBG, i, 1)) * (IIf(i < 7, 8, 14) - i)
Next
fctBlnCheckSum = (intCheckSum Mod 11) <> 0
End Function