如何让 IsNumeric 检查整个 string/variable 而不是只检查第一个字符?
How do I make IsNumeric check the whole string/variable instead of just the first character?
我正在使用 IsNumeric
检查变量的一部分是否为数字。不幸的是,它似乎只检查字符串部分的第一个字符而不是整个位。
它目前接受 Q123 1234567 和 QWER 1QWERTYR(以及其他变体)。虽然我需要前 4 个字符都是字母,而其他字符都是数字。
我不知道我还缺少什么。请尽可能添加额外的评论,我对 vba 的理解仍然低于基本水平。
Dim ConNr As String
Dim Space As String
Dim Four As String
Dim Six As String
Dim One As String
Dim Container As String
ConNr = Me.txtContainer.Value
Space = " "
Four = Left(Me.txtContainer.Value, 4)
Four = UCase(Four)
Six = Mid(Me.txtContainer.Value, 5, 6)
One = Right(Me.txtContainer.Value, 1)
'Check if all 4 are letters
If IsNumeric(Four) = True Then
MsgBox "First 4 need to be letters."
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Four Letters " + Four
'Check if 6 characters are numbers
If IsNumeric(Six) = False Then
MsgBox "4 Letters followed by 6 numbers."
'MsgBox "These Six " + Six
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Six Numbers " + Six
'Last number is number
If IsNumeric(One) = False Then
MsgBox "Last character needs to be a number."
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Last Number " + One
ConNr = Four & Space & Six & Space & One
Container = ConNr
End If
End If
End If
根据 JvdV 编辑
当我尝试 "[A-Za-z][A-Za-z][A-Za-z][A-Za-z] ###### #"
时,输出为空。
我不想强迫用户使用正确的格式。 (大写字母,空格。)但是 4 letters/7 个数字是必需的。
Dim ConNr As String: ConNr = Me.txtContainer.Value
If ConNr Like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z]#######" Then ‘Without spaces, else it doesn’t post.
Container = UCase(ConNr)
Else
MsgBox "YOU FAILED."
Me.txtContainer.SetFocus
Exit Sub
End If
‘Output should become ASDF 123456 7. Currently gives me ASDF1234567.
根据我的评论,特此提供一个简单的示例代码来演示 Like
运算符的使用:
Sub Test()
Dim str As String: str = "QWER 1234567"
Dim arr As Variant: arr = Split(str, " ")
If arr(0) Like "[A-Z][A-Z][A-Z][A-Z]" And IsNumeric(arr(1)) Then
Debug.Print str & " is passed!"
End If
End Sub
顺便说一句,如果你想允许大写和小写,你可以使用:[A-Za-z][A-Za-z][A-Za-z][A-Za-z]
编辑
如果您要寻找 4 个字母字符的模式,然后是 space,然后是 6 位数字,您甚至可以做更简单的事情:
Sub Test()
Dim str As String: str = "QWER 123456"
If str Like "[A-Z][A-Z][A-Z][A-Z] ######" Then
Debug.Print str & " is passed!"
End If
End Sub
如果要包含另一个 space/digit,请扩展表达式。您说的是:
"ConNr = Four & Space & Six & Space & One"
所以 [A-Z][A-Z][A-Z][A-Z] ###### #
在那种情况下对你有用。
根据您的评论,您不想对用户强制使用特定格式,只要他们的字符串中有 4 个字母字符和 7 个数字字符即可。以任何形式。
所以我想,既然要放space的地方太多了,最好用Application.Substitute
去掉它们。您的代码可能如下所示:
If Application.Substitute(Me.txtContainer.Value, " ", "") Like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z]#######" Then
Debug.Print str & " is passed!"
End If
如果您不想强制大写但又想 return 它,那么请使用 UCase
函数一次将整个字符串设为大写字母!
Debug.Print UCase(Application.Substitute(Me.txtContainer.Value, " ", ""))
很难隐藏这很像 RegEx
的事实。
在这个解决方案中,合同编号格式的批准由一个函数提供,returns 如果编号正确则为真,否则为假。如果数字不好,该函数会告诉它出了什么问题。如果发现可接受,则调用过程继续执行该程序。请注意,该函数可容纳缺失或多余的空格并将小写字母转换为大写字母。
Option Explicit
Private Sub TestConNumber()
Dim ConNr As String
' ConNr = Me.txtContainer.Value
ConNr = "QAAK 781234 x"
If GetConNumber(ConNr) Then
MsgBox "The Contract number is " & ConNr
End If
End Sub
Private Function GetConNumber(ConNr As String) As Boolean
' return Not True if incorrect
Dim Fun As Boolean ' function return value
Dim Nr As String
Dim Msg As String
Dim Arr(1 To 3) As String
Nr = UCase(Replace(ConNr, " ", ""))
If Len(Nr) = 11 Then
Arr(1) = Left(Nr, 4)
If Arr(1) Like "[A-Z][A-Z][A-Z][A-Z]" Then
If IsNumeric(Right(Nr, 7)) Then
Arr(2) = Mid(Nr, 2, 6)
Arr(3) = Right(Nr, 1)
ConNr = Join(Arr)
Fun = True
Else
Msg = "The last 7 digits must be numbers."
End If
Else
Msg = "The first 4 characters must be non-numeric"
End If
Else
Msg = "Input must have 11 characters"
End If
If Not Fun Then
MsgBox Msg, vbExclamation, "Wrong input"
End If
GetConNumber = Fun
End Function
我正在使用 IsNumeric
检查变量的一部分是否为数字。不幸的是,它似乎只检查字符串部分的第一个字符而不是整个位。
它目前接受 Q123 1234567 和 QWER 1QWERTYR(以及其他变体)。虽然我需要前 4 个字符都是字母,而其他字符都是数字。
我不知道我还缺少什么。请尽可能添加额外的评论,我对 vba 的理解仍然低于基本水平。
Dim ConNr As String
Dim Space As String
Dim Four As String
Dim Six As String
Dim One As String
Dim Container As String
ConNr = Me.txtContainer.Value
Space = " "
Four = Left(Me.txtContainer.Value, 4)
Four = UCase(Four)
Six = Mid(Me.txtContainer.Value, 5, 6)
One = Right(Me.txtContainer.Value, 1)
'Check if all 4 are letters
If IsNumeric(Four) = True Then
MsgBox "First 4 need to be letters."
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Four Letters " + Four
'Check if 6 characters are numbers
If IsNumeric(Six) = False Then
MsgBox "4 Letters followed by 6 numbers."
'MsgBox "These Six " + Six
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Six Numbers " + Six
'Last number is number
If IsNumeric(One) = False Then
MsgBox "Last character needs to be a number."
Me.txtContainer.SetFocus
Exit Sub
Else
'MsgBox "Last Number " + One
ConNr = Four & Space & Six & Space & One
Container = ConNr
End If
End If
End If
根据 JvdV 编辑
当我尝试 "[A-Za-z][A-Za-z][A-Za-z][A-Za-z] ###### #"
时,输出为空。
我不想强迫用户使用正确的格式。 (大写字母,空格。)但是 4 letters/7 个数字是必需的。
Dim ConNr As String: ConNr = Me.txtContainer.Value
If ConNr Like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z]#######" Then ‘Without spaces, else it doesn’t post.
Container = UCase(ConNr)
Else
MsgBox "YOU FAILED."
Me.txtContainer.SetFocus
Exit Sub
End If
‘Output should become ASDF 123456 7. Currently gives me ASDF1234567.
根据我的评论,特此提供一个简单的示例代码来演示 Like
运算符的使用:
Sub Test()
Dim str As String: str = "QWER 1234567"
Dim arr As Variant: arr = Split(str, " ")
If arr(0) Like "[A-Z][A-Z][A-Z][A-Z]" And IsNumeric(arr(1)) Then
Debug.Print str & " is passed!"
End If
End Sub
顺便说一句,如果你想允许大写和小写,你可以使用:[A-Za-z][A-Za-z][A-Za-z][A-Za-z]
编辑
如果您要寻找 4 个字母字符的模式,然后是 space,然后是 6 位数字,您甚至可以做更简单的事情:
Sub Test()
Dim str As String: str = "QWER 123456"
If str Like "[A-Z][A-Z][A-Z][A-Z] ######" Then
Debug.Print str & " is passed!"
End If
End Sub
如果要包含另一个 space/digit,请扩展表达式。您说的是:
"ConNr = Four & Space & Six & Space & One"
所以 [A-Z][A-Z][A-Z][A-Z] ###### #
在那种情况下对你有用。
根据您的评论,您不想对用户强制使用特定格式,只要他们的字符串中有 4 个字母字符和 7 个数字字符即可。以任何形式。
所以我想,既然要放space的地方太多了,最好用Application.Substitute
去掉它们。您的代码可能如下所示:
If Application.Substitute(Me.txtContainer.Value, " ", "") Like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z]#######" Then
Debug.Print str & " is passed!"
End If
如果您不想强制大写但又想 return 它,那么请使用 UCase
函数一次将整个字符串设为大写字母!
Debug.Print UCase(Application.Substitute(Me.txtContainer.Value, " ", ""))
很难隐藏这很像 RegEx
的事实。
在这个解决方案中,合同编号格式的批准由一个函数提供,returns 如果编号正确则为真,否则为假。如果数字不好,该函数会告诉它出了什么问题。如果发现可接受,则调用过程继续执行该程序。请注意,该函数可容纳缺失或多余的空格并将小写字母转换为大写字母。
Option Explicit
Private Sub TestConNumber()
Dim ConNr As String
' ConNr = Me.txtContainer.Value
ConNr = "QAAK 781234 x"
If GetConNumber(ConNr) Then
MsgBox "The Contract number is " & ConNr
End If
End Sub
Private Function GetConNumber(ConNr As String) As Boolean
' return Not True if incorrect
Dim Fun As Boolean ' function return value
Dim Nr As String
Dim Msg As String
Dim Arr(1 To 3) As String
Nr = UCase(Replace(ConNr, " ", ""))
If Len(Nr) = 11 Then
Arr(1) = Left(Nr, 4)
If Arr(1) Like "[A-Z][A-Z][A-Z][A-Z]" Then
If IsNumeric(Right(Nr, 7)) Then
Arr(2) = Mid(Nr, 2, 6)
Arr(3) = Right(Nr, 1)
ConNr = Join(Arr)
Fun = True
Else
Msg = "The last 7 digits must be numbers."
End If
Else
Msg = "The first 4 characters must be non-numeric"
End If
Else
Msg = "Input must have 11 characters"
End If
If Not Fun Then
MsgBox Msg, vbExclamation, "Wrong input"
End If
GetConNumber = Fun
End Function