19

I have an Excel table that contains some data. By using next vba code I'm trying to filter only blank cells in some fields and delete these rows

ActiveSheet.Range("$A$1:$I$" & lines).AutoFilter Field:=7, Criteria1:= _
        "="
ActiveSheet.Range("$A$1:$I$" & lines).AutoFilter Field:=8, Criteria1:= _
        "="
ActiveSheet.Range("$A$1:$I$" & lines).AutoFilter Field:=9, Criteria1:= _
        "="
ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.rows.Count - 1).rows.Delete
ActiveSheet.ShowAllData

It works only if I have blank cells in this columns. But I faced with a problem, when I do not have blank cells, and by using above code all my range is removing from the sheet. How to avoid this issue? Should I change my filter condition or something else?

1
  • Hey, can you help me define what is the "lines" you mention in your code Commented Sep 26, 2021 at 19:51

2 Answers 2

45

Use SpecialCells to delete only the rows that are visible after autofiltering:

ActiveSheet.Range("$A$1:$I$" & lines).SpecialCells _
    (xlCellTypeVisible).EntireRow.Delete

If you have a header row in your range that you don't want to delete, add an offset to the range to exclude it:

ActiveSheet.Range("$A$1:$I$" & lines).Offset(1, 0).SpecialCells _
    (xlCellTypeVisible).EntireRow.Delete
Sign up to request clarification or add additional context in comments.

5 Comments

Method explained on this page combines the filtering and offsetting and deleting, which makes reading code easier. With rRange 'Filter, offset(to exclude headers) and delete visible rows .AutoFilter Field:=lCol, Criteria1:=strCriteria .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With
just precede the delete line with a try-catch block as it throws an error if you try to delete an empty selection : On Error Resume Next
Hi just wondering how can I modify the above line to delete hidden rows after filter ?
What is lines? is it a vba row variable or does the programmer define it?
I believe ,EntireRow.Delete may fail if something is not visible, e.g. hidden column or hidden sheet. .Rows.Delete seems to work. Some people use .Select prior to .EntireRow.Delete can solve some problems but has a similar problem with visibility. See here: answers.microsoft.com/en-us/msoffice/forum/all/…
21

As an alternative to using UsedRange or providing an explicit range address, the AutoFilter.Range property can also specify the affected range.

ActiveSheet.AutoFilter.Range.Offset(1,0).Rows.SpecialCells(xlCellTypeVisible).Delete(xlShiftUp)

As used here, Offset causes the first row after the AutoFilter range to also be deleted. In order to avoid that, I would try using .Resize() after .Offset().

1 Comment

Good solution, but works only with autofiltered views, not with data marked up as table... It would be just perfect, if it would do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.