String 两个字符串虽然相同,但在比较时并不相等
大家好,VB有点问题,我正在读Excel中的一个字符串,并将其与另一个进行比较,当我看到MSGBox时,它们看起来一模一样,但VB没有识别出它们是相同的,这让我很困惑,谢谢String 两个字符串虽然相同,但在比较时并不相等,string,excel,vba,String,Excel,Vba,大家好,VB有点问题,我正在读Excel中的一个字符串,并将其与另一个进行比较,当我看到MSGBox时,它们看起来一模一样,但VB没有识别出它们是相同的,这让我很困惑,谢谢 Sub runit() Dim indicator As Integer Dim actual As String Dim tmp As String tmp = "3. AIRCRAF" Sheets("Sheet2").Select For i = 3 To 1200 actual = Le
Sub runit()
Dim indicator As Integer
Dim actual As String
Dim tmp As String
tmp = "3. AIRCRAF"
Sheets("Sheet2").Select
For i = 3 To 1200
actual = Left(Cells(i, 1).Text, 10)
If i = 203 Then
MsgBox actual & tmp
End If
If actual = tmp Then
MsgBox i
Cells(i, 1).Select
ActiveCell.Range("A1:M997").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
tmp = "zzZZxxXXedc"
End If
Next
Sheets("Sheet3").Select
tmp = "H."
indicator = 0
For j = 1 To 600
If tmp = actual Then
indicator = 1
Cells(j, 1).Select
tmp = "zzZZxxXXedc"
ActiveCell.Range("A1:M1200").Select
Selection.ClearContents
Cells(1, 1).Select
End If
Next
If indicator = 0 Then
actual = Left(Cells(j, 1).Value, 2)
Rows(j + 1).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End If
End Sub
在更严重的情况下,目视检查不足以确定导致对比错误的罪魁祸首。不同unicode代码点的空格或类似符号序列可能会蒙蔽眼睛。因此,请投资一个函数,该函数接受一个字符串并使用Hex、AscW、Mid和padding返回其“hexdump”,然后将其应用于从您的tmp中查找重命名的对象,这会有被重新使用的风险,并会应用于实际操作。这就是最终的效果。还是不知道出了什么事。我想如果我在两个变量上都使用Mid,它可能会起作用,而且确实起作用了。我想也许有人能解释原因
Sub runit()
Dim indicator As Integer
Dim actual As String
Dim tmp As String
tmp = Mid("3. AIRCRAFT STATUS", 1, 10)
Sheets("Sheet2").Select
For i = 3 To 1200
actual = Mid(Cells(i, 1).Text, 1, 10)
If i = 203 Then
MsgBox (actual) & " " & (tmp)
End If
If actual = tmp Then
MsgBox i
Cells(i, 1).Select
ActiveCell.Range("A1:M997").Select
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
tmp = "zzZZxxXXedc"
End If
Next
Sheets("Sheet3").Select
tmp = Mid("H. MAJOR INSP REQUIREMENTS:", 1, 5)
indicator = 0
For j = 1 To 600
If tmp = actual Then
indicator = 1
Cells(j, 1).Select
tmp = "zzZZxxXXedc"
ActiveCell.Range("A1:M1200").Select
Selection.ClearContents
Cells(1, 1).Select
End If
Next
If indicator = 0 Then
actual = Mid(Cells(j, 1).Text, 1, 5)
Rows(j + 1).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End If
End Sub