Stop searching. Start learning and developing your excel skills.
Macro
VBA
Formula
Function
Shortcut
Tricks

» » VBA To Delete Filtered Rows

VBA To Delete Filtered Rows

June 03, 2017 |
This is also related to below queries:
  1. filter and delete rows in excel
  2. excel vba delete filtered rows but not header
  3. vba delete visible rows except header
  4. vba code to delete rows based on criteria
  5. excel vba delete filtered rows in table
The Code:
Dim i, j As Long

j = Cells(Rows.Count, "A").End(xlUp).Row

Sheets("Sheet1").Range("A1").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=IF(ISERROR(MATCH(""ABC"",C8:C8,0)),0,1)"
i = ActiveCell.Value
ActiveCell.Value = i
Selection.ClearContents

If i > 0 Then
Range("$A$1:$Q$1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$Q$" & j).AutoFilter Field:=8, Criteria1:="ABC"
ActiveSheet.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
ActiveSheet.ShowAllData
End If

How it works?
  1. The data begins at "A1"
  2. The code will read total rows
  3. Then assign a temporary cell in last cell in first row to check whether is there any data or not to be filtered. Then clear the temporary cell value
  4. If got data to filter then filter then delete filtered rows. After filter, show all data
  5. If no data to filter then do nothing

Where:
  1. j = Total Rows
  2. "ABC" = Criteria to filter is "ABC". Change to your requirement
  3. C8:C8 = Criteria to be filtered in column number 8 or column H. Change to your requirement
  4. "$A$1:$Q$1" = header to filter. Change to your requirement
  5. AutoFilter Field:=8 is column number 8 or column H. Change to your requirement

No comments:

Post a Comment