比较 VBA 中的 2 个数字
Comparing 2 Numbers in VBA
我正在尝试比较 2 个 3 位数。这是我当前使用嵌套 Ifs
的代码
If Mid(Num1, 1, 1) = Mid(Num2, 1, 1) Then
'Check first number against first number
If Mid(Num1, 2, 1) = Mid(Num2, 2, 1) Then
'Check second number against second number
If Mid(Num1, 3, 1) = Mid(Num2, 3, 1) Then
'Check third number against third number
Digits = 3
Else
Digits = 2
End If
而这只是其中的一小部分。另外,我还需要检查它们匹配的顺序。因此,无论是完全匹配,所有 3 位数字以任何顺序匹配,还是 1 或 2 位数字以任何顺序匹配。
问题是我有很多使用此方法的 If 语句,因为我必须比较每个数字组合以检查 1 位、2 位、3 位等是否匹配。有没有更好的方法?
可以通过简单的 for
循环简化为 function
Private Function digitMatch(ByVal num1 as String, ByVal num2 as String) As Byte
' num1 and num2 are strings, because of presumption they can start with 0
' (i.e. 042 is valid 3 digit number format, otherwise they can be integers as well)
Dim i As Byte
Dim matches As Byte: matches = 0
For i = 1 To Len(num1)
If InStr(1, num2, Mid(num1, i, 1)) <> 0 Then
matches = matches + 1
End If
Next i
digitMatch = matches
End Function
so eg. digitMatch(023, 053)
would return 2
or digitMatch(123, 321)
would return 3
试试这个(只有当 'curNum' 和 'WinningNumber' 都是 3 位长时才能正常工作):
'straight match
If curNum = WinningNumber Then
M = 3
s = 3
'matched the first 2 straight
ElseIf InStr(1, WinningNumber, Left(curNum, 2)) > 0 Then
M = 2
s = 2
If InStr(1, WinningNumber, Right(curNum, 1)) > 0 Then M = M + 1
'matched the last 2 straight
ElseIf InStr(2, WinningNumber, Right(curNum, 2)) > 0 Then
M = 2
s = 2
If InStr(1, WinningNumber, Left(curNum, 1)) > 0 Then M = M + 1
'any other scenario
Else
s = 0
For i = 1 To 3
n = Mid(WinningNumber, i, 1)
If InStr(1, curNum, n) > 0 Then
M = M + 1
End If
Next
End If
Debug.Print "Matched digits: " & M
Debug.Print "Straight: " & s
我确定有更好的方法,但这是我快速编写它的最简单方法。
在我的回答中,我 return 匹配的数字,因此您可以检查是否有以及哪些。此外,它适用于任意数量的数字。
Public Function CheckForMatch(ByVal curNum As String, ByVal winNumber As String) As String
Dim i As Long, j As Long
Dim hit As String
hit = vbNullString
For i = 1 To Len(curNum)
j = InStr(1, winNumber, Mid(curNum, i, 1), vbTextCompare)
If j > 0 Then
hit = hit & Mid(curNum, i, 1)
End If
Next i
CheckForMatch = hit
End Function
Public Sub Test()
Dim check As String
check = CheckForMatch("75214", "13672")
If Len(check) > 0 Then
Debug.Print "Numbers " & check & " are a match."
' 721
Else
Debug.Print "No match. Sorry."
End If
End Sub
注意:这里使用 InStr()
的灵感来自于 Rawplus 在我之前给出的答案。
我正在尝试比较 2 个 3 位数。这是我当前使用嵌套 Ifs
的代码If Mid(Num1, 1, 1) = Mid(Num2, 1, 1) Then
'Check first number against first number
If Mid(Num1, 2, 1) = Mid(Num2, 2, 1) Then
'Check second number against second number
If Mid(Num1, 3, 1) = Mid(Num2, 3, 1) Then
'Check third number against third number
Digits = 3
Else
Digits = 2
End If
而这只是其中的一小部分。另外,我还需要检查它们匹配的顺序。因此,无论是完全匹配,所有 3 位数字以任何顺序匹配,还是 1 或 2 位数字以任何顺序匹配。
问题是我有很多使用此方法的 If 语句,因为我必须比较每个数字组合以检查 1 位、2 位、3 位等是否匹配。有没有更好的方法?
可以通过简单的 for
循环简化为 function
Private Function digitMatch(ByVal num1 as String, ByVal num2 as String) As Byte
' num1 and num2 are strings, because of presumption they can start with 0
' (i.e. 042 is valid 3 digit number format, otherwise they can be integers as well)
Dim i As Byte
Dim matches As Byte: matches = 0
For i = 1 To Len(num1)
If InStr(1, num2, Mid(num1, i, 1)) <> 0 Then
matches = matches + 1
End If
Next i
digitMatch = matches
End Function
so eg.
digitMatch(023, 053)
would return2
ordigitMatch(123, 321)
would return3
试试这个(只有当 'curNum' 和 'WinningNumber' 都是 3 位长时才能正常工作):
'straight match
If curNum = WinningNumber Then
M = 3
s = 3
'matched the first 2 straight
ElseIf InStr(1, WinningNumber, Left(curNum, 2)) > 0 Then
M = 2
s = 2
If InStr(1, WinningNumber, Right(curNum, 1)) > 0 Then M = M + 1
'matched the last 2 straight
ElseIf InStr(2, WinningNumber, Right(curNum, 2)) > 0 Then
M = 2
s = 2
If InStr(1, WinningNumber, Left(curNum, 1)) > 0 Then M = M + 1
'any other scenario
Else
s = 0
For i = 1 To 3
n = Mid(WinningNumber, i, 1)
If InStr(1, curNum, n) > 0 Then
M = M + 1
End If
Next
End If
Debug.Print "Matched digits: " & M
Debug.Print "Straight: " & s
我确定有更好的方法,但这是我快速编写它的最简单方法。
在我的回答中,我 return 匹配的数字,因此您可以检查是否有以及哪些。此外,它适用于任意数量的数字。
Public Function CheckForMatch(ByVal curNum As String, ByVal winNumber As String) As String
Dim i As Long, j As Long
Dim hit As String
hit = vbNullString
For i = 1 To Len(curNum)
j = InStr(1, winNumber, Mid(curNum, i, 1), vbTextCompare)
If j > 0 Then
hit = hit & Mid(curNum, i, 1)
End If
Next i
CheckForMatch = hit
End Function
Public Sub Test()
Dim check As String
check = CheckForMatch("75214", "13672")
If Len(check) > 0 Then
Debug.Print "Numbers " & check & " are a match."
' 721
Else
Debug.Print "No match. Sorry."
End If
End Sub
注意:这里使用 InStr()
的灵感来自于 Rawplus 在我之前给出的答案。