我能想到有两件事可以防止两个字符串相等:
(1) 您可能想研究一下 VBA 中的不同比较选项。查看以下内容以获得更多帮助:
https://msdn.microsoft.com/en-us/library/8t3khw5f.aspx.
(2) 如果您在国际上工作,那么a 看起来像a,但实际上它们并不相同。第一个可以使用Selection.Value = ChrW(97)(使用拉丁字母)来实现,第二个是Selection.Value = ChrW(1072)(来自西里尔键盘)。它们看起来(视觉上)相同,但实际上与 VBA 不同。
注意:不要在直接窗口中尝试第二个,因为您可能只会看到 ChrW(1072) 的 ?。而是将这两个值(如上所示)分配给工作表中的两个不同单元格,您会看到它们看起来相同,但实际上并非如此:
Public Sub Comparison()
Sheets(1).Cells(2, 1).Value = ChrW(97)
Sheets(1).Cells(2, 2).Value = ChrW(1072)
Sheets(1).Cells(2, 3).Formula = "=$A$1=$B$2"
End Sub
作为最后一个选项,您可以逐个字母进行比较以手动找出差异。下面的 sub 将做到这一点(有点冗长):
Option Compare Binary
Public Sub StringCompare()
Dim strText As String
Dim strSentence As String
strText = "Table 4.662: MIMCAP Design Rules (Part 2)" 'First string to compare to.
strSentence = "Table 4.662: MIMCAP Design Rules (Part 2)" 'Second string to compare to.
Dim lngLetterCount As Long
For lngLetterCount = 1 To IIf(Len(strText) > Len(strSentence), Len(strText), Len(strSentence))
Debug.Print "Letter " & Right(" " & lngLetterCount, 3) & ": " _
& Mid(strText, lngLetterCount, 1) _
& " (" & Right("00" & AscW(Mid(strText, lngLetterCount, 1)), 3) & ")" _
& " - " _
& Mid(strSentence, lngLetterCount, 1) _
& " (" & Right("00" & AscW(Mid(strSentence, lngLetterCount, 1)), 3) & ") " _
& IIf(AscW(Mid(strText, lngLetterCount, 1)) = AscW(Mid(strSentence, lngLetterCount, 1)), "", "<-- Err")
Next lngLetterCount
If Len(strText) >= lngLetterCount Then
Debug.Print "The text string is longer than the sentence string. Ther is no match for '" & Mid(strText, lngLetterCount) & "' in sentence."
End If
If Len(strSentence) >= lngLetterCount Then
Debug.Print "The sentence string is longer than the text string. Ther is no match for '" & Mid(strSentence, lngLetterCount) & "' in text."
End If
End Sub