EXCEL批量删除空行的VBA怎么写

2025-01-08 06:27:48
推荐回答(2个)
回答1:

方法是:选择包含需要删除的数据区域,比如A2:D10,然后按F5键,打开“定位”对话框,单击“定位条件”,打开“定位条件”对话框,选择“空值”,可以批量选择空行,然后单击右键,选择删除即可。

excel批量删除空行方法二:
我们可以在旁边的空白列输入1,然后下拉,作为辅助列。然后开启“筛选”,筛选“空白”,在筛选状态删除这些空白行,最后关闭筛选。
excel批量删除空行方法三:
如果工作表中有大量的空行,可以使用VBA代码实现。方法是:按ALT+F11,打开VBE编辑器,插入——模块,复制下面的代码,然后按F5键运行。
Sub 批量删除空行()
Dim LastRow As Long
Dim r As Long
LastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
Application.ScreenUpdating = False
For r = LastRow To 1 Step -1
If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then Rows(r).Delete
Next r
Application.ScreenUpdating = True
End Sub

回答2:

万能代码:

Sub 删除所有空行和空列()
For Each Sheet In Sheets
Sheet.Select
Sheet.UsedRange.UnMerge

Call DeleteEmptyRows
Call DeleteEmptyColumns
Rows.AutoFit
Columns.AutoFit

Next
End Sub

Sub DeleteEmptyRows()
Dim LastRow As Long
Dim r As Long
LastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
Application.ScreenUpdating = False
For r = LastRow To 1 Step -1
If Application.WorksheetFunction.CountA(Rows(r)) = 0 _
Then Rows(r).Delete
Next r
Application.ScreenUpdating = True
End Sub

Sub DeleteEmptyColumns()
Dim LastColumn As Long
Dim r As Long
LastColumn = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
Application.ScreenUpdating = False
For r = LastColumn To 1 Step -1
If Application.WorksheetFunction.CountA(Columns(r)) = 0 _
Then Columns(r).Delete
Next r
Application.ScreenUpdating = True
End Sub