Delete filtered rows on a sheet with unknown number of rows

MaxNoob

New Member
Joined
Apr 18, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I recently started learning excel vba and so far I have relied heavily on the 'record macro' option for a lot of the formatting work. I've run into an issue, where I need the code to delete filtered rows (while keeping the headers), but the number of rows changes between sheets so I can't code for a specific range.
I need the code to autofilter for the designated criteria, then delete all those rows. I know the code needs to find the last row as part of the process but I'm not sure how to go about changing the recorded code. The screenshot shows the recorded code. The AJ part of the "ActiveSheet.Range("$A$1:$AJ$114127")" can change to anything depending on the sheet.
Any suggestions on how to fix this?
 

Attachments

  • Delete autofiltered range.JPG
    Delete autofiltered range.JPG
    96.2 KB · Views: 20

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,
You have posted your macro to Filter ...
But after filtering you do get a range of visible cells only ...
So you can test following to adjust your range by Offset 1 to skip headers and with Resize to get the number of rows :
VBA Code:
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)

Hope this will help
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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