Delete Row if

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Please help below trying to say if 2 cells in same row = specific values then delete row.

VBA Code:
ElseIf (CellI) Like "* + collection*" And (CellJ) Like "*askwh*" Then

Option Explicit

Public intDeleteNonBO As Integer
Sub DeleteAnyNonBO()
    Dim wb     As Workbook
    Dim ws     As Worksheet
    Dim CellI   As String, Col As String, CellJ As String
    Dim LRow   As Long
    Dim i      As Integer, x As Integer

    intDeleteNonBO = 0
   
    With Application
        .ScreenUpdating = False
        .Calculation = xlManual
        .DisplayAlerts = False
        .EnableEvents = False
    End With
   
    Set wb = Workbooks("2024BackOrderReport.xlsm")
    Set ws = wb.Worksheets("Data")
    LRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
   
    For i = LRow To 2 Step -1
        On Error Resume Next
        Col = "A"
        CellI = LCase(ws.Cells(i, 9).Value)
        CellJ = LCase(ws.Cells(i, 10).Value)
        If IsDate(ws.Cells(i, Col).Value) And ws.Cells(i, Col).Value = Date Then
            If CellI Like "no space" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*fit on*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*bag not*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*loading picking error*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no room*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no more room*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*error*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*failed*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*Not Picked*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*Not Loaded*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no space*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*wouldn`t fit*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*wrong site*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*driver forgot*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*driver didn`t*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*Would not fit on Truck*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*Vehicle at capacity*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "* & collection*" And (CellJ) Like "*askwh*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellJ) Like "*fr*" Then
                ws.Cells(i, "A").EntireRow.Delete
            End If
        End If
    Next i
   
    intDeleteNonBO = 1
   
    With Application
        .ScreenUpdating = True
        .Calculation = xlAutomatic
        .DisplayAlerts = True
        .EnableEvents = True
    End With
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You don't tell us what its is doing or not doing.
"Like" is case sensitive and you have lower-cased CellI & CellJ variables but some of your comparatives are mixed case ie "Not Picked" and "Not Loaded" etc.
It will only even get to those If statements if Column A contains a date and that date = Today, is that actually the case ? If you F8 through the code does it meet that test ?
 
Upvote 0
Replace this portion by this code.

VBA Code:
  If CellI Like "no space" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*fit on*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*bag not*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*loading picking error*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no room*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no more room*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*error*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*failed*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*Not Picked*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf CellI Like "*Not Loaded*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*no space*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*wouldn`t fit*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*wrong site*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*driver forgot*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*driver didn`t*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*Would not fit on Truck*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "*Vehicle at capacity*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellI) Like "* & collection*" And (CellJ) Like "*askwh*" Then
                ws.Cells(i, "A").EntireRow.Delete
            ElseIf (CellJ) Like "*fr*" Then
                ws.Cells(i, "A").EntireRow.Delete
            End If

I have example here. Create a Array as given below with all elements in the macro . Use this code. I have added few elements. Add all required elements.
VBA Code:
Ary = Array("no space", "fit on", "bag not", "loading picking error", "no room", "no more room", "error", "failed")
For Each S In Ary
If InStr(1, cellI, S) > 0 Then
ws.Cells(i, "A").EntireRow.Delete
Exit For
End If
Next
 
Upvote 0
You don't tell us what its is doing or not doing.
"Like" is case sensitive and you have lower-cased CellI & CellJ variables but some of your comparatives are mixed case ie "Not Picked" and "Not Loaded" etc.
It will only even get to those If statements if Column A contains a date and that date = Today, is that actually the case ? If you F8 through the code does it meet that test ?
On the VBA watch both cells match what I have specified but the code skips over it so the row does not delete?
 
Upvote 0
Can you clarify which line is actually failing.
If it is the date line, does Column A contain Date & Time ?
If so try using:
Rich (BB code):
        If IsDate(ws.Cells(i, Col).Value) And Int(ws.Cells(i, Col).Value2) = Date Then

If it's one of the other conditions tell us which one and show us the values in CellI & CellJ when it fails.
If it's a case mismatch issue then @kvsrinivasamurthy's suggested approach may well solve it for you.

Note in post 1 you have a discrepancy:
One contains "+" the other "&"
Which would be in CellI ?
ElseIf (CellI) Like "* + collection*" And (CellJ) Like "*askwh*" Then
ElseIf (CellI) Like "* & collection*" And (CellJ) Like "*askwh*" Then


 
Upvote 0
Solution

Forum statistics

Threads
1,224,862
Messages
6,181,465
Members
453,045
Latest member
Abraxas_X

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top