Excel VBA – 需要删除列B中填充参考错误的单元格值所在的行

我有一个循环到我的代码的底部,成功地循环我的数据,并清除列H = 0的所有行。

但是,列B中有几个单元格显示#REF! 。 我也希望这个循环删除这些行的方式与列H中的0相同。

我想我的问题是不知道如何引用这些types的错误。 治疗#REF! 像一个string似乎没有工作。

谢谢!

 Sub test() Dim currentSht As Worksheet Dim lastRow As Long Dim lastCol As Long Dim startCell As Range Dim r As Integer Set startCell = Sheets("Sheet1").Range("A1") Set currentSht = ActiveWorkbook.Sheets("Sheet1") lastRow = startCell.SpecialCells(xlCellTypeLastCell).Row '<~~ Not sure why, but do not use "Set" when defining lastRow lastCol = startCell.SpecialCells(xlCellTypeLastCell).Column For r = 1 To lastRow Step -1 If currentSht.Cells(r, "H").Value = 0 Or currentSht.Cells(r, "B").Text = "#REF!" Then Rows(r).Select Selection.EntireRow.Delete End If Next r currentSht.Range(startCell, currentSht.Cells(lastRow, lastCol)).Select End Sub 

我想我看到你的问题:

  For r = 1 To lastRow Step -1 

将该行更改为

 For r = lastrow to 1 Step -1 

这个代码如何:

 Sub Delete0() Dim F As Integer Dim Y As Integer Dim RngCount As Range Set RngCount = ActiveSheet.Range("H:H") Y = Application.WorksheetFunction.CountA(RngCount) For F = Y To 1 Step -1 If IsError(ActiveSheet.Range("H" & F)) Then ActiveSheet.Rows(F).EntireRow.Delete ElseIf ActiveSheet.Range("H" & F).Value = 0 Then ActiveSheet.Rows(F).EntireRow.Delete End If Next F End Sub