在我的宏中,我想删除完全空白的行。不是一列中有某些内容的行,因为那仍然是有价值的信息。
我在互联网上看了很多,但是如果有人想出了一个功能,它可以删除整个空白行,而不仅仅是删除一些很棒的列中缺少的行。
(信息仅在A到N列和大约7千行上)。我没有为此开发任何代码,因为我真的很沮丧。
答案 0 :(得分:2)
您可以使用此...
Sub ClearEmptyRows()
Dim r As Long, lastrow As Long, WS As Worksheet, killRng As Range
Set WS = ActiveSheet
lastrow = WS.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set killRng = WS.Rows(Rows.Count)
For r = 1 To lastrow
If Application.WorksheetFunction.CountA(WS.Rows(r)) = 0 Then
Set killRng = Union(killRng, WS.Rows(r))
End If
Next r
killRng.Delete
End Sub
此代码对新手有一些注释,因为这是遍历行并执行某事(删除,突出显示,隐藏等)的通用例程。
Delete
。Union
留空,因此我将killRng
设置为最初的最后一行(希望始终为空白),然后宏可以继续{{1 }}。可以通过包含if语句来解决此问题,但这需要宏检查每一行上是否存在范围。答案 1 :(得分:0)
如果您只想删除空行而又不关心格式化,那将是非常快的事情。
Sub RemoveEmptyRows()
Dim results As Variant, Target As Range
Dim c As Long, r As Long, n As Long
Set Target = Worksheets("Sheet1").UsedRange
If Target.Count > 0 Then
ReDim results(1 To Target.Rows.Count, 1 To Target.Columns.Count)
For r = 1 To Target.Rows.Count
If WorksheetFunction.CountA(Target.Rows(r)) > 0 Then
For c = 1 To Target.Columns.Count
n = n + 1
results(n, c) = Target.Cells(r, c).Value
Next
End If
Next
End If
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Target.Value = results
Application.ScreenUpdating = xlCalculationAutomatic
Application.ScreenUpdating = False
End Sub