VBA - Delete rows based on values from two columns (delete when just one value is met)

thomasuponor

New Member
Joined
Sep 13, 2018
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have below macro that looks into column B and deletes row if one of the numbers are present.
I would want the macro to include also column C and delete the rows if these criteria are present - "MLC", "PEX"

Have tried to add an extra Set rng = ws.Range("C3:C" & lastRow) but does not work.

VBA Code:
Dim ws As Worksheet
    Dim rng As Range
    Dim lastRow As Long

    Set ws = ActiveWorkbook.Sheets("Bilag_1")

    lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row

    Set rng = ws.Range("B3:B" & lastRow)

    ' filter and delete all but header row
    With rng
        .AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=Array("1033080", "1033081", "1033179", "1033277", "1033084", "1033186", "1033270", "1033086", "1059576", "1059577")
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    ' turn off the filters
    ws.AutoFilterMode = False
 
Try this

VBA Code:
Sub Myfilter()
   
  Dim ws As Worksheet
  Dim rng As Range, myPEX As Range, myMLC As Range
  Dim lastRow As Long
   
  Set ws = ActiveWorkbook.Sheets("Bilag_1")
  Set myPEX = ws.UsedRange.Find("PEX", LookIn:=xlValues)
  Set myMLC = ws.UsedRange.Find("MLC", LookIn:=xlValues)
    
    lastRow = ws.Range("B" & ws.Rows.count).End(xlUp).row
   
  Set rng = ws.Range("B3:B" & lastRow)
   
  If Not myPEX Is Nothing Then
      ws.Rows(myPEX.row).Delete
  End If

  If Not myMLC Is Nothing Then
      ws.Rows(myMLC.row).Delete
  End If
   
  ws.AutoFilterMode = False
   
' filter and delete all but header row
  With rng
      .AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=Array("1033080", "1033081", "1033179", "1033277", "1033084", "1033186", "1033270", "1033086", "1059576", "1059577")
      .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
  End With

' turn off the filters
   ws.AutoFilterMode = False
 
End Sub
Great job - THX!
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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