删除列中有错误的所有行
我正在处理VBA代码,部分代码需要删除表格中“J”为“#N / A”的文本中的所有行。 我已经写了一个代码,但是在debugging时出现Type不匹配的错误。
这是代码
Dim i As Long For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1 If Cells(i, 10) = "#N/A" Then Cells(i, 1).EntireRow.Delete Next i
使用带有xlSpecialCellsValue常量的Range.SpecialCells方法作为xlErrors,可以快速识别列J中的所有单元格,并带有错误。 没有透露是否单元格是公式或types常量,所以我添加了代码来检查通过xlCellType枚举types。
Sub del_error_rows() With Worksheets("Sheet3") On Error Resume Next With .Columns(10).SpecialCells(xlCellTypeFormulas, xlErrors) .EntireRow.Delete End With With .Columns(10).SpecialCells(xlCellTypeConstants, xlErrors) .EntireRow.Delete End With On Error GoTo 0 End With End Sub
如果不存在具有该特定错误configuration的单元,那么On Error Resume Next
是必要的。 在这种情况下,SpecialCells将是Nothing
并且您必须绕过尝试不处理任何事情的任何错误。
试试这个代码:
Dim i As Long For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1 If Cells(i, 10).Text = "#N/A" Then Cells(i, 1).EntireRow.Delete Next i