两个字符串在比较时不相等,尽管它们是相同的

时间:2011-04-20 15:32:25

标签: string excel excel-vba vba

Hey guys / gals,在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 = 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

2 个答案:

答案 0 :(得分:0)

在更为讨厌的情况下,视觉检查不足以找出导致比较的罪魁祸首。 (用于不同的unicode代码点的空格或类似字形的序列)可能会使人眼前一亮。因此,请投入一个函数,该函数接受一个字符串并返回其'hexdump'(使用Hex,AscW,Mid和padding)并将其应用于LookFor(从您的tmp重命名,可以重复使用)和实际的。

答案 1 :(得分:0)

这就是最终的工作。还是不知道出了什么问题。想想如果我在这两个变量上使用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
相关问题