VBA: Find Columns and Delete Cells

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I had a couple queries.

I have sheet that requires to delete some rows. I have 2 headers (which are never in same columns) that I need to find and then delete rows. The Number of rows also varies each day.

  1. Approval Status - Delete all rows with status that does not equal "Pending"
  2. Created - Delete all rows with dates that today and future dates. Only dates allowed are older then today.

Book1
ABCDEFG
1Header 1Header 2Approval StatusCreatedHeader 5Header 6Header 7
2Approved3/1/2022
3Rejected3/16/2022
4Pending1/1/2022
Sheet1


Thank you for any help.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Maybe this. I have assumed you want it actually deleting rather than filtering as you said delete and that there are no data that would fulfill BOTH criteria

VBA Code:
Dim ws As Worksheet
Dim MyApp As Long, MyCtd As Long
Dim myrng As Range
Dim mydte As Date


Set ws = Sheets("Sheet1") 'change to be your sheetname
Set myrng = ws.UsedRange
mydte = Date
Debug.Print myrng.address
Debug.Print mydte

 MyApp = myrng.Find(what:="*Approval*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
 MyCtd = myrng.Find(what:="*Created*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column

 ws.UsedRange.AutoFilter Field:=MyApp, Criteria1:="<>Pending", Operator:=xlAnd, Field:=MyCtd, Criteria2:=">" & Date
 
' Approval Status - Delete all rows with status that does not equal "Pending"
' Created - Delete all rows with dates that today and future dates. Only dates allowed are older then today.
 
 ws.UsedRange.SpecialCells(xlVisible).EntireRow.Delete
 
 ws.AutoFilterMode = False
 
Upvote 0
Maybe this. I have assumed you want it actually deleting rather than filtering as you said delete and that there are no data that would fulfill BOTH criteria

VBA Code:
Dim ws As Worksheet
Dim MyApp As Long, MyCtd As Long
Dim myrng As Range
Dim mydte As Date


Set ws = Sheets("Sheet1") 'change to be your sheetname
Set myrng = ws.UsedRange
mydte = Date
Debug.Print myrng.address
Debug.Print mydte

 MyApp = myrng.Find(what:="*Approval*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
 MyCtd = myrng.Find(what:="*Created*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column

 ws.UsedRange.AutoFilter Field:=MyApp, Criteria1:="<>Pending", Operator:=xlAnd, Field:=MyCtd, Criteria2:=">" & Date
 
' Approval Status - Delete all rows with status that does not equal "Pending"
' Created - Delete all rows with dates that today and future dates. Only dates allowed are older then today.
 
 ws.UsedRange.SpecialCells(xlVisible).EntireRow.Delete
 
 ws.AutoFilterMode = False
you are correct, I would need the rows deleted.

For some reason when I tried your code, I get an "Delete method of Range class failed"
 
Upvote 0
Another option...
EDITED

VBA Code:
Sub Filter_Delete()
    Dim i As Long, j As Long
    i = WorksheetFunction.Match("Approval Status", Rows(1), 0)
    j = WorksheetFunction.Match("Created", Rows(1), 0)
    
    With Range("A1").CurrentRegion
        .AutoFilter i, "<>Pending"
        .Offset(1).EntireRow.Delete
        .AutoFilter
        .AutoFilter j, ">=" & CLng(Date)
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Last edited:
Upvote 0
Solution
you are correct, I would need the rows deleted.

For some reason when I tried your code, I get an "Delete method of Range class failed"
this line
VBA Code:
 ws.UsedRange.SpecialCells(xlVisible).EntireRow.Delete

should be
VBA Code:
 ws.UsedRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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