我有一些代码贯穿我的工作表并突出显示所有带有公式的单元格。这部分代码工作正常,但是,如果工作表中没有包含公式的单元格,则代码会崩溃。我想要做的是,如果电子表格中没有公式,则在其中放置一个 if 语句将结束代码。我试图遍历单元格并检查每个单元格是否有一个公式,但也会崩溃。所以我想做的是修复 if 语句。
任何帮助将不胜感激。
高亮代码
'Apply yellow highlight to all formula cells.
Dim ws As Worksheet
Dim rng As Range
Set ws = ActiveSheet
For Each rng In ws.Cells.SpecialCells(xlCellTypeFormulas)
rng.Interior.ColorIndex = 36
Next rng
带有 if 语句的代码
'Apply yellow highlight to all formula cells.
Dim ws As Worksheet
Dim rng As Range
Set ws = ActiveSheet
c = 0
For Each cell in ws.Cells
If cell.HasFormula = True Then
c= c + 1
End If
Next cell
If c > 0 Then
For Each rng In ws.Cells.SpecialCells(xlCellTypeFormulas)
rng.Interior.ColorIndex = 36
Next rng
Else MsgBox ("No formulas in this worksheet")
End If