Excel VBA基于包含文本部分的单元格值删除行

时间:2017-08-30 14:16:17

标签: excel vba excel-vba

我想根据包含单元格中“upg”或“upgrade”字样的单元格值删除行。当这个词出现时,它不是单元格内的唯一单词。

以下是我的代码的一部分,不会删除任何内容:

Sub DeleteValues()


Dim i As Integer
Dim MFG_wb As Workbook
Dim Dep As Integer



Set MFG_wb = Workbooks.Open _
("C:\Users\rosipov\Desktop\eliran\MFG - GSS\MFG Daily\Fast Daily " & Format(Now(), "ddmmyy") & ".xlsx", _
UpdateLinks:=False, IgnoreReadOnlyRecommended:=True)
MFG_wb.Sheets("Aleris").Activate

Dep = MFG_wb.Sheets("Aleris").Range("B2").End(xlDown).Row


For i = Dep To 2 Step -1
    Cells(i, 2).Select
    If Not (Selection.Value = "& upg &" Or Selection.Value = "*& upgrade &*") Then
        Rows(i).Delete
    End If
Next i



End Sub

问题在于:

For i = Dep To 2 Step -1
    Cells(i, 2).Select
    If Not (Selection.Value = "& upg &" Or Selection.Value = "*& upgrade &*") Then
        Rows(i).Delete
    End If
Next i

1 个答案:

答案 0 :(得分:5)

使用INSTR():

For i = Dep To 2 Step -1
     If Instr(MFG_wb.Sheets("Aleris").Cells(i, 2).Value,"upg")>0 Or Instr(MFG_wb.Sheets("Aleris").Cells(i, 2).Value,"upgrade") >0 Then
        MFG_wb.Sheets("Aleris").Rows(i).Delete
     End If
Next i