2011-04-20 52 views
0

嘿家伙/ 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 

回答

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 
0

在更恶劣的情况下,目视检查不足以识别导致比较的罪魁祸首。 (Sequences of)空格或类似的glyphys用于不同的unicode码点可能会欺骗眼睛。因此,投资一个函数,该函数接受一个字符串并返回它的'hexdump'(使用Hex,AscW,Mid和padding),并将其应用于LookFor(从您的tmp重命名,这是有风险的重用)和实际。

相关问题