跳到主要内容

如何过滤列表并删除Excel中其余的隐藏行或可见行?

对于过滤列表,您可能需要删除隐藏或可见行,以便仅保留有用数据。 在本文中,我们将向您展示删除Excel中已过滤列表的其余隐藏或可见行的方法。

使用VBA代码删除活动工作表中的隐藏行
通过选择所有可见单元格删除过滤列表的可见行
使用 Kutools for Excel 轻松删除过滤列表的隐藏或可见行


使用VBA代码删除活动工作表中的隐藏行

本节将向您展示VBA代码,以删除活动工作表中的隐藏行。 请执行以下操作。

1.激活需要删除隐藏行的工作表,然后按 其他 + F11 同时打开 Microsoft Visual Basic应用程序 窗口。

2.在“ Microsoft Visual Basic for Applications”窗口中,单击“ 插页 > 模块。 然后将下面的VBA代码复制并粘贴到“模块”窗口中。

VBA代码:删除隐藏的行

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。 按 F5 键来运行代码。 如果活动工作表中有隐藏行,则运行代码后,将弹出一个对话框,告诉您已删除了多少个隐藏行。 点击 OK 按钮删除隐藏的行。 看截图:

doc删除休息1

否则,运行代码后,您将获得以下对话框。

doc删除休息1

备注:上面的VBA代码不仅可以删除过滤列表的隐藏行,还可以删除您之前手动隐藏的隐藏行。


通过选择所有可见单元格功能删除过滤列表的可见行

要删除过滤列表的可见行,请执行以下操作。

1.选择所有过滤出的行,然后按 F5 键打开 转到 对话框,然后单击 Special 按钮。 看截图:

doc删除休息1

2.在 定位条件 对话框,检查 仅可见细胞 选项,然后单击 OK 按钮。

doc删除休息1

3.现在,所有可见行均被选中,右键单击所选内容,然后单击“确定”。 删除行.

doc删除休息1

到目前为止,所有可见行均已从过滤列表中删除。


使用 Kutools for Excel 轻松删除过滤列表的隐藏或可见行

对于许多Excel用户来说,以上两种方法可能不是理想的解决方案,在这里我们为您介绍一个方便的工具。 随着 删除隐藏(可见)行和列 实用程序 Kutools for Excel,您可以轻松删除Excel中所选范围/工作表,活动工作表或所有工作表中的隐藏行。

申请前 Kutools for Excel首先下载并安装.

1.如果您只想删除过滤列表的隐藏或可见行,请手动选择过滤范围,然后单击 库工具 > 删除 > 删除隐藏(可见)行和列。 看截图:

2.在 删除隐藏(可见)的行和列 对话框中,保留 在选定范围内 选中 在看 下拉列表(您可以根据需要选择其他选项),然后选中 在选项 删除类型 部分,并在 详细类型 部分,检查 可见行 or 隐藏的行 您需要的选项。 最后点击 OK 按钮。

3.然后会弹出一个对话框,告诉您已删除了多少行,请单击 OK 按钮。

  如果您想免费试用(30天)此实用程序, 请点击下载,然后按照上述步骤进行操作。


使用 Kutools for Excel 删除过滤列表的隐藏或可见行

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
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