所以它是9999999或者返回值为True的必需Medicaid格式化字符串。
样品:
AZ12345Z
NP54321J
EM17345P
到目前为止,我有两个功能在一起工作,但我弄错了逻辑!!
谢谢
Public Function isAlpha(cChar As Integer) As Boolean
'returns true if its a alphabetic character
isAlpha = IIf((cChar >= 65 And cChar <= 90) Or (cChar >= 97 And cChar <= 122), True, False)
End Function
Public Function CheckMedicaidIDFormat(strMedicaidID As String) As Boolean
Dim blnResult As Boolean
If strMedicaidID = "99999999" or If Len(strMedicaidID) = 8 And isAlpha(Left(strMedicaidID, 2)) = True And IsNumeric(Mid(strMedicaidID, 3, 5)) = True And isAlpha(Right(strMedicaidID, 1)) = True Then
blnResult = True
Else
blnResult = False
End If
CheckMecicaidIDFormat = blnResult
End Function
答案 0 :(得分:6)
虽然RegEx
是针对此类问题的良好通用解决方案,但在这种情况下,简单的Like
比较会做
Function IsValid(strIn As String) As Boolean
IsValid = (strIn Like "[A-Z][A-Z]#####[A-Z]") Or strIn = "99999999"
End Function
答案 1 :(得分:3)
像这样的东西
Sub Test()
MsgBox IsValid("AZ12345Z")
MsgBox IsValid("1AZ12345Z")
End Sub
测试功能
Function IsValid(strIn As String) As Boolean
If strIn = "99999999" Then
IsValid = True
Else
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "^[A-Z]{2}\d{5}[A-Z]$"
IsValid = .Test(strIn)
End With
End If
End Function
答案 2 :(得分:0)
VBA支持使用COM VBScript.RegExp
对象的正则表达式,如下所示:
Dim regex As VBScript.RegExp
Set regex = New VBScript.RegExp ' or CreateObject("VBScript.RegExp")
regex.Pattern = "^[A-Za-z][A-Za-z]\d\d\d\d\d[A-Za-z]$"
If medicId = "99999999" Or regex.Test( medicId ) Then
' code to handle valid Medicaid ID here
End If
答案 3 :(得分:0)
正则表达式非常适合这种情况,但如果您不想使用它(许多人出于各种原因这样做),很可能没有。
你在CheckMediacaidIDFormat函数的if语句中犯了一个小错误。 '如果'被提到两次,你忘记了一些括号。我会这样表达:
Public Function CheckMedicaidIDFormat(strMedicaidID As String) As Boolean
Dim blnResult As Boolean
If strMedicaidID = "99999999" Or _
(Len(strMedicaidID) = 8 And _
isAlpha(Left(strMedicaidID, 2)) = True And _
IsNumeric(Mid(strMedicaidID, 3, 5)) = True And _
isAlpha(Right(strMedicaidID, 1)) = True) _
Then
blnResult = True
Else
blnResult = False
End If
CheckMecicaidIDFormat = blnResult
End Function
请注意,我使用换行符(vba中的空格后跟下划线)以提高可读性,并围绕'And'条件使用括号。如果您愿意,可以不使用换行符,但不能没有括号。