我有下面的Sub,它遍历了许多工作表,应该删除范围内的任何空行。有时它会工作得很好,但有时它会给我一个“没有找到单元错误”并失败。我不明白为什么。
错误出现在Set r = Range("B2:B" & counter).SpecialCells(xlCellTypeBlanks)行
我试图通过一个if语句来捕获这个错误,但是通过这样做,例程不会删除任何东西,因为r当然什么都不是。
考虑到每次运行宏时空白行可能位于不同的位置,是否有更简单的方法从多个选项卡中删除空白行。
Sub RemoveBlanks()
Dim ws As Worksheet
Dim counter As Long
Dim r As Range
Dim cell As Range
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Summary" Then
ws.Activate
counter = ws.Range("A" & Rows.Count).End(xlUp).Row
Set r = Range("B2:B" & counter).SpecialCells(xlCellTypeBlanks)
If r Is Nothing Then
GoTo test
End If
r.EntireRow.Delete shift:=xlShiftUp
End If
test: Next ws
Worksheets("Summary").Activate
End Sub发布于 2020-09-12 18:07:27
如果没有空单元格,您将得到一个错误,如果有错误,可以继续下一步。
Sub Button1_Click()
Dim sh As Worksheet
For Each sh In Sheets
On Error Resume Next
If sh.Name <> "Summary" Then
With sh
.Range("B2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End If
Next
End Subhttps://stackoverflow.com/questions/63858986
复制相似问题