VBA Coding to select cell under Headings when using filter

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
141
I am using a filter to show all "BB" transactions and delete the rest of the rows that are not "BB". Here is what I recorded when I did it:

'Set autofilter on Column E for "BB" and delete all other rows
Range("A1").Select
Sheets("PPDR_BB").Select
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$T$15865").AutoFilter Field:=5, Criteria1:=Array( _
"#N/A", "Overtime", "PAR", "Premiums", "Regular"), Operator:=xlFilterValues
Range("A634").Select
Range(Selection, Selection.End(xlToRight)).Select
Range("A634:P634").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
ActiveSheet.Range("$A$1:$T$633").AutoFilter Field:=5

My question is how can I adjust the coding to delete the rows under the headings that I do not require as the starting row will be variable.

Thanks
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,919
Office Version
2007
Platform
Windows
Try this

Code:
Sub test()
  Dim sh As Worksheet, lr As Long
  Sheets("PPDR_BB").Select
  Set sh = ActiveSheet
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  lr = sh.Range("E" & Rows.Count).End(xlUp).Row
  sh.Range("A1:T" & lr).AutoFilter Field:=5, Criteria1:=Array( _
    "#N/A", "Overtime", "PAR", "Premiums", "Regular"), Operator:=xlFilterValues
  sh.AutoFilter.Range.Offset(1).EntireRow.Delete
  sh.ShowAllData
End Sub
 

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
141
Try this

Code:
Sub test()
  Dim sh As Worksheet, lr As Long
  Sheets("PPDR_BB").Select
  Set sh = ActiveSheet
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  lr = sh.Range("E" & Rows.Count).End(xlUp).Row
  sh.Range("A1:T" & lr).AutoFilter Field:=5, Criteria1:=Array( _
    "#N/A", "Overtime", "PAR", "Premiums", "Regular"), Operator:=xlFilterValues
  sh.AutoFilter.Range.Offset(1).EntireRow.Delete
  sh.ShowAllData
End Sub
I am getting a compile Error:

Block If without End if. I have tried putting the end if in several places with no success.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,919
Office Version
2007
Platform
Windows
Did you copy complete the macro of post #2 ?

Did you modify some of the original macro?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,919
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,078,442
Messages
5,340,305
Members
399,366
Latest member
ahmed elsaid

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top