且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查工作表中是否有任何公式

更新时间:2023-11-25 15:15:46

您可以在代码中使用错误处理.

You may use error handling in the code.

错误恢复时继续将继续在下一行执行,即使发生错误也不会中断脚本.
On Error Goto 0 会在当前过程中禁用已启用的错误处理程序,并将其重置为Nothing.

On Error Resume Next will continue execution on the next line without interrupting the script even when error occurs.
On Error Goto 0 disables enabled error handler in the current procedure and resets it to Nothing.

Sub test()

    Dim ws As Worksheet
    Dim rng As Range, cell As Range

    Set ws = ActiveSheet
    On Error Resume Next
    Set rng = ws.Cells.SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "No cells found"
        Exit Sub
    End If

    For Each cell In rng
        cell.Interior.ColorIndex = 36
    Next

End Sub