Suppose we have a long list in worksheet (1000 rows or more), and we do filter to show only some cells of the list by some criteria. If we want to keep the visible cells and remove the hidden rows to reduce redundant data and the size of file, how can we do? On the other side, if we want to remove the visible cells in the filter and keep the hidden rows, how can we do? In this article, we will introduce you some methods to remove hidden or visible rows in filter per your demands.
We have a list of numbers.
Create a filter that only show rows where value is greater than 300.
Method 1: Delete/Remove Hidden Rows Not in Filter
Before applying below method, make sure create a copy of your original file in case of improper operating.
Step 1: Click File in ribbon, and select Info.
Step 2: Click arrow button in Check for Issues, select Inspect Document.
Step 3: Verify that Document Inspector window displays. Drag the scroll bar to the bottom, verify that Hidden Rows and Columns option is checked by default.
Step 4: Click Inspect button in the bottom. Verify that Remove All button appears in Hidden Rows and Columns, and ‘Number of hidden rows found:5’ displays.
Step 5: Click Remove All. Verify that all hidden rows and columns containing data were removed.
Step 6: Click Close button to quit Document Inspector window. Go back to worksheet, verify that hidden rows are removed properly, only show rows in filter are kept.
Method 2: Delete/Remove Hidden Rows Not in Filter by VBA Code
Step 1: On current visible worksheet, right click on sheet name tab to load Sheet management menu. Select View Code, Microsoft Visual Basic for Applications window pops up.
Or you can enter Microsoft Visual Basic for Applications window via Developer->Visual Basic. You can also press Alt + F11 keys simultaneously to open it.
Step 2: In Microsoft Visual Basic for Applications window, click Insert->Module, enter below code in Module1:
Sub DeleteHiddenRows() Set myRows = Intersect(ActiveSheet.Range("A:A").EntireRow, ActiveSheet.UsedRange) If myRows Is Nothing Then Exit Sub For Each oneRow In myRows.Columns(1).Cells If oneRow.EntireRow.Hidden Then If myRange Is Nothing Then Set myRange = oneRow Else Set myRange = Union(myRange, oneRow) End If End If Next If Not myRange Is Nothing Then MsgBox myRange.Count & " hidden rows have been removed.", , "Removing Hidden Rows" myRange.EntireRow.Delete Else MsgBox "No hidden rows were found", , "Remove Hidden Rows" End If End Sub
Step 3: Save the codes, see screenshot below. And then quit Microsoft Visual Basic for Applications.
Step 4: Click Developer->Macros to run Macro. Select ‘RemoveHiddenRows’ and click Run.
Step 5: Verify that Remove Hidden Rows message pops up. And ‘5 hidden rows have been found. All were removed.’ shows on the message.
Step 6: Click OK and check the result. Verify that hidden rows are removed properly.
Method 3: Delete/Remove Visible Rows in Filter by Go To Special
Step 1: Select visible cells, then press F5 to load Go To dialog, click Special.
Step 2: On Go To Special dialog, check on Visible cells only option, then click OK.
Step 3: Verify that visible cells are activated. Right click to load menu, select ‘Delete Row’.
Step 4: Verify that visible cells are removed. Expand filter, verify that only hidden rows are shown.