在删除引用错误的同时删除单元格

问题描述:

嗨,我想删除单元格的同时删除引用错误,但我的代码似乎不工作。它删除单元格,但不删除引用错误。需要帮助。非常感谢你!在删除引用错误的同时删除单元格

Sub DeleteCells() 


    Dim rngError As Range 
    Dim R As Range 
    Set rng = Nothing 
    On Error Resume Next 
    Set R = Application.InputBox("Select cells To be deleted", Type:=8) 
    Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) 
    On Error GoTo 0 
    'delete means cells will move up after deleting that entire row 
    'rngError.EntireRow.ClearContents means that the contents will clear, leaving a blank cell for that entire row 

    If TypeName(R) <> "Range" Then 
     Exit Sub 
    Else 
    R.Delete 
    For Each cell In Range("A1:Z100") 
    If Not rngError Is Nothing Then 
    rngError.Delete 

End If 

Next 

End If 



End Sub 
+0

如果你得到一个'#REF!'错误,因为偏移或类似的东西尝试引用单元格关闭工作表(例如' = offset($ C $ 3,-5,-10)'然后清除这些单元格;不要删除它们,如果删除它们并且下面的单元格向上移动,那么这些单元格中的公式现在有相同的问题。 cell正在引用一个你删除的单元格,它现在有一个'#REF!'错误。 – Jeeped

这将在列删除公式误差的

Sub del_err() 
    Dim rng As Range 
    On Error GoTo er 
    Set rng = Columns("A:A").SpecialCells(xlCellTypeFormulas, 16) 
    If Not rng Is Nothing Then 
     rng.Delete Shift:=xlUp 
     Exit Sub 
    End If 
er:  MsgBox "No errors found" 
End Sub 
+0

@ Niva-你应该回复评论和答案。 – Davesexcel