Skip to main content

How to filter a list and delete the rest hidden or visible rows in Excel?

Author: Siluvia Last Modified: 2020-04-28

For a filtered list, you may need to delete the hidden or visible rows in order to keep only the useful data. In this article, we will show you methods of deleting the rest hidden or visible rows of a filtered list in Excel.

Delete hidden rows in active worksheet with VBA code
Delete visible rows of filtered list with selecting all visible cells
Easily delete hidden or visible rows of filtered list with Kutools for Excel


Delete hidden rows in active worksheet with VBA code

This section will show you VBA code to delete hidden rows in active sheet. Please do as follows.

1. Activate the worksheet you need to delete hidden rows, press Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module. And then copy and paste the below VBA code into the Module window.

VBA code: Delete hidden rows

Sub RemoveHiddenRows()
	Dim xRow As Range
	Dim xRg As Range
	Dim xRows As Range
	On Error Resume Next
	Set xRows = Intersect(ActiveSheet.Range("A:A").EntireRow, ActiveSheet.UsedRange)
	If xRows Is Nothing Then Exit Sub
		For Each xRow In xRows.Columns(1).Cells
			If xRow.EntireRow.Hidden Then
				If xRg Is Nothing Then
					Set xRg = xRow
				Else
					Set xRg = Union(xRg, xRow)
				End If
			End If
		Next
		If Not xRg Is Nothing Then
			MsgBox xRg.Count & " hidden rows have been deleted", , "Kutools for Excel"
			xRg.EntireRow.Delete
		Else
			MsgBox "No hidden rows found", , "Kutools for Excel"
		End If
	End Sub

3. Press the F5 key to run the code. If there are hidden rows in active sheet, after running the code, a dialog box will pop up to tell you how many hidden rows have been deleted. Click the OK button to delete the hidden rows. See screenshot:

doc delete rest 1

Otherwise, you will get the following dialog box after running the code.

doc delete rest 1

Note: the above VBA code not only can delete hidden rows of filtered list, but also delete hidden rows which you have manually hidden before.


Delete visible rows of filtered list with selecting all visible cells feature

For deleting visible rows of filtered list, please do as follows.

1. Select all filtered out rows, and press F5 key to open the Go To dialog box, then click the Special button. See screenshot:

doc delete rest 1

2. In the Go To Special dialog box, check the Visible cells only option, and then click the OK button.

doc delete rest 1

3. Now all visible rows are selected, right click the selection, and then click Delete Rows.

doc delete rest 1

Until now, all visible rows are deleted from the filtered list.


Easily delete hidden or visible rows of filtered list with Kutools for Excel

The above two methods may be not the desire solutions for many Excel users, here we introduce you a handy tool. With the Delete Hidden (Visible) Rows & Columns utility of Kutools for Excel, you can easily delete hidden rows in selected range/sheets, active sheet or all worksheets in Excel.

Before applying Kutools for Excel, please download and install it firstly.

1. If you only want to delete hidden or visible rows of a filtered list, please select the filtered range manually, and then click Kutools > Delete > Delete Hidden (Visible) Rows & Columns. See screenshot:

2. In the Delete Hidden (Visible) Rows & Column dialog box, keep the In selected Range selected in the Look in drop-down list (you can select other options as you need), check the Rows option in the Delete type section, and in the Detailed type section, check Visible rows or Hidden rows option as you need. And finally click the OK button.

3. Then a dialog box pops up to tell you how many rows have been deleted, please click the OK button.

  If you want to have a free trial (30-day) of this utility, please click to download it, and then go to apply the operation according above steps.


Delete hidden or visible rows of filtered list with Kutools for Excel

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (7)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi this VBA code is awesome useful.
This comment was minimized by the moderator on the site
Thanks for the info. This has been instructive. Please is there a way to filter and delete for specific numbers in rows of up to 1 million? Can the code above be modified to do so?
This comment was minimized by the moderator on the site
Hi,
Sorry did not test the code in such huge row data. Maybe you can make a copy of your data and test if the code can work.
This comment was minimized by the moderator on the site
VBA code to Delete hidden rows worked perfectly with no fiddling. THANK YOU!!
This comment was minimized by the moderator on the site
Feedback re: the macro for hidden row deletions - this takes too long to run on 900k rows to be useful. 2+ hours on an OC'd Threadripper 1950X and still running (had to end task). Any way to optimize it to use multiple cores or is this a VBA limitation?
This comment was minimized by the moderator on the site
Hi,
The code has been optimized. Please have a try. Thank you for your comment.

Sub RemoveHiddenRows()
Dim xFlag As Boolean
Dim xStr, xTemp As String
Dim xDiv, xMod As Long
Dim I, xCount, xRows As Long
Dim xRg, xCell, xDRg As Range
Dim xArr() As String
On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
Set xRg = Intersect(ActiveSheet.Range("A:A").EntireRow, ActiveSheet.UsedRange)
If xRg Is Nothing Then Exit Sub
xRows = xRg.Rows.Count
Set xRg = xRg(1)
xFlag = True
xTemp = ""
xCount = 0
For I = 1 To xRows
Set xCell = xRg.Offset(I - 1, 0)
Do While xFlag
If xCell.EntireRow.Hidden Then
xStr = xCell.Address
xFlag = False
Else
GoTo Ctn
End If
Loop
If xCell.EntireRow.Hidden Then
xTemp = xStr & "," & xCell.Address
End If
If Len(xTemp) > 171 Then
xCount = xCount + 1
ReDim Preserve xArr(1 To xCount)
xArr(xCount) = xStr
xStr = xCell.Address
Else
xStr = xTemp
End If
Ctn:
Next
xCount = xCount + 1
ReDim Preserve xArr(1 To xCount)
xArr(xCount) = xStr
For I = xCount To 1 Step -1
If I = 1 Then
xStr = Mid(xArr(I), InStr(xArr(I), ",") + 1, Len(xArr(I)) - InStr(xArr(I), ","))
Else
xStr = xArr(I)
End If
If xDRg Is Nothing Then
Set xDRg = Range(xStr)
Else
Set xDRg = Union(xDRg, Range(xStr))
End If
If (Len(xDRg.Address) >= 244) Or (xCount = 1) Then
xDRg.EntireRow.Delete
Set xDRg = Nothing
End If
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Awesome, thank you! Will review at my next opportunity / need for this and reply back.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations