预计代码将运行循环并删除列b中所有相应的单元格行,并且没有单元格值。在运行代码时,Error' 424'显示所需的对象。为什么这个错误通常会出现,因为我在if语句中得到了很多错误?
Sub cleaning()
Dim s As Worksheet
Dim r As Range
Set s = ThisWorkbook.ActiveSheet
Set r = s.Range("B:B")
r.Activate
For Each c In r
If c.Value Is Nothing Then
s.Range("B" & c.Row).EntireRow.Delete
End If
Next c
End Sub
答案 0 :(得分:2)
试试这个:
Sub cleaning()
Dim s As Worksheet
Dim lastRow As Integer
Set s = ThisWorkbook.ActiveSheet
lastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For c = lastRow To 1 Step -1
If Cells(c, 2).Value = "" Then
s.Range("B" & c).EntireRow.Delete
End If
Next c
End Sub