Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Delete Rows Based on Cell Value containing Part of a Text

Tags:

excel

vba

I want to delete rows based on cell value that contains the words "upg" or "upgrade" inside the cell. when this word are there, it is not the only words inside the cell.

Here is part of my code, that not deletes anything:

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

Here is the problem:

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
like image 901
Rafael Osipov Avatar asked Oct 26 '25 08:10

Rafael Osipov


1 Answers

Use 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
like image 95
Scott Craner Avatar answered Oct 28 '25 23:10

Scott Craner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!