比较字符串值时,If语句跳过正文

时间:2018-08-30 15:13:30

标签: excel

我正在尝试根据关键字删除列。即使字符串完全匹配且变量类型匹配,if语句也会跳过主体。我在生产线上添加了星号,给我带来麻烦以及手表结果。如果有人能给我一个原因,我将不胜感激。

Dim sourcebook As Workbook
Dim sourcesheet As Worksheet
Dim lastcol As Long
'Dim currentCell As String



Set sourcebook = ActiveWorkbook
Set sourcesheet = sourcebook.Sheets("Sheet1")

Set ws = Application.activesheet

With ws
    If WorksheetFunction.CountA(Cells) > 0 Then
        lastcol = Cells.Find(what:="*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
    End If
End With

Set chkrng = activesheet.Range("E1", Cells(1, lastcol))

'time keywords
t1 = "Min. Time   L1 Phase"
t2 = "Max. TimeL1 Phase"
t3 = "Min. TimeL2 Phase"
t4 = "Max. TimeL2 Phase"
t5 = "Min. TimeL3 Phase"
t6 = "Max. TimeL3 Phase"
t7 = "Min. Time3 Phase Sy..."
t8 = "Max. Time3 Phase Sy..." 

For Each cell In chkrng
    ***If cell.Value = t1 Then cell.EntireColumn.Delete***
Next
Watch :   :cell.Value : "Min. Time   L1 Phase" : Variant/String 
Watch :   :t1 : "Min. Time   L1 Phase" : Variant/String : 

更新:我无法修复原始代码,但由于其他人遇到类似问题,因此我不愿附加我所做的事情。

For i = 4 To lastcol - 1
    CurrValue = Range("A1").Offset(0, i).Value
    If InStr(1, CurrValue, "Time") > 0 Then
        Columns(i + 1).Delete
    End If
Next i

0 个答案:

没有答案