开发者

Two Strings not Equal when Compared although they are the same

开发者 https://www.devze.com 2023-02-27 03:16 出处:网络
Hey guys/gals, having some trouble in VB, I am reading in a string from Excel and comparing it to another, when I see the MSGBox they look identical, yet VB is not recognizing them as the same and its

Hey guys/gals, having some trouble in VB, I am reading in a string from Excel and comparing it to another, when I see the MSGBox they look identical, yet VB is not recognizing them as the same and its been screwing me up thanks.

    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, Transp开发者_如何转开发ose:=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


In the more nasty cases, visual inspection is not sufficient to identify the culprit that screws up a comparison. (Sequences of) whitespace or similar glyphys for different unicode codepoints may fool the eye. So invest in a function that takes a string and returns its 'hexdump' (using Hex, AscW, Mid, and padding) and apply that to LookFor (renamed from your tmp, which is riskily reused) and to actual.


This is what ended up working. Still don't know what was wrong. Figured if I used Mid on both the variables maybe it'd work and it did. Just thought maybe someone could explain why.

    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
0

精彩评论

暂无评论...
验证码 换一张
取 消