在行和删除单元格中查找#REF

我在Excel 2010中有一个电子表格,有时会有一个引用错误。 我想清除特定行中的单元格; 即。 清除行7中包含参考错误的所有单元格。 我遇到了一些麻烦。

Sub Check_Ref() Dim rng As Range Dim rngError As Range Dim cell as 'something Set rng = Sheets("All Projects").Range("AE7:AK7") Dim rngError As Range Set rngError = rng.SpecialCells(xlCellTypeFormulas, xlErrors) If Not rngError Is Nothing Then For Each cell In rngError 'Clear the cell Next End If End Sub 

这是我迄今为止。 我不知道如何清理细胞。 任何帮助将是伟大的。

不需要循环:

 Sub Check_Ref() Dim rng As Range Dim rngError As Range Set rng = Sheets("All Projects").Range("AE7:AK7") Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) If Not rngError Is Nothing Then rngError.Clear End If End Sub 

编辑#1

如果在工作表上找不到错误,请禁用错误:

 Sub Check_Ref() Dim rng As Range Dim rngError As Range Set rng = Sheets("All Projects").Range("AE7:AK7") On Error Resume Next Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) On Error GoTo 0 If Not rngError Is Nothing Then rngError.ClearContents End If End Sub 

鉴于你想隔离#REF错误会build议你更具体的处理从SpecialCells xlErrors构造的xlErrors:

 Sub Check_Err() Dim rng As Range Dim rng2 As Range Dim rngError As Range Set rng = Sheets("All Projects").Range("AE7:AK7") On Error Resume Next Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) On Error GoTo 0 If Not rngError Is Nothing Then For Each rng2 In rngError If rng2.Value2 = CVErr(xlErrRef) Then rng2.ClearContents Next End If End Sub