我有一段代碼貫穿我的工作表並突出顯示其中有公式的所有單元。但是,如果在工作表中沒有包含公式的單元格,代碼的這部分工作正常,那麼代碼將崩潰。我想要做的是,如果在電子表格中沒有公式,那麼放入一個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
僅供參考,不VBA的解決方案,但你可以使用條件格式突出顯示公式的單元格如下所述:http://j-walk.com/ss /excel/usertips/tip045.htm – ozandlb