删除参考错误时删除单元格

时间:2015-08-15 05:38:03

标签: excel vba excel-vba

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

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

1 个答案:

答案 0 :(得分:0)

这将删除A列中的公式错误

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