首页 > 解决方案 > 比较字符串值VBA时,如果语句跳过正文

问题描述

我正在尝试根据关键字删除列。即使字符串完全匹配并且变量类型匹配,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

标签: excel

解决方案


推荐阅读