2012-07-02 30 views
4

在Excel中,可以找到引用空單元格的公式。錯誤檢查和跟蹤空單元格

例如

A1 = 1

B1 =空

C1 = 0.5

D1 = A1 + B1 + C1

D1將正確計算的值以是1.5 但是,錯誤檢查將確定公式D1中存在錯誤,並且可以爲該單元格繪製箭頭。

我需要提醒用戶,如果他們有一個空單元格的引用。

當我嘗試使用該功能來記錄自己,我得到這個

With Application.ErrorCheckingOptions 
    .EvaluateToError = False 
    .TextDate = False 
    .NumberAsText = False 
    .InconsistentFormula = False 
    .OmittedCells = False 
    .UnlockedFormulaCells = False 
    .ListDataValidation = False 
    .InconsistentTableFormula = False 
End With 

ActiveSheet.ClearArrows 

只檢查空單元格的選項是正確的,但設置了「跟蹤」功能的實際執行時完全忽略。是否有任何方法可以自動發生,然後檢查測試結果?

功能是「公式選項卡」,「錯誤檢測」「公式引用空單元」,「跟蹤空單元」

+0

我可能是錯的,但我懷疑有直接的方式調用跟蹤空錯誤。如果一段代碼告訴你公式中的哪個單元格爲空,會有幫助嗎? –

+0

+ 1好問:) –

回答

1

這裏是一個VBA方法來檢查公式引用空細胞。這不僅會告訴你它是哪個公式,而且哪個單元格是空的。

爲了測試這一點,在Sheet,單元格A1把這個公式

=F1+G1+H1 

保持H1空填充F1和G1

在細胞A5,把這個公式

=A1*D5 

保持Cell D5爲空。

現在將此代碼粘貼到模塊中。

Option Explicit 

Sub Sample() 
    Dim ws As Worksheet 
    Dim RngFormulas As Range, fCell As Range, _ 
    DPrcd As Range, aCell As Range 

    Set ws = Sheets("Sheet1") 

    With ws 
     On Error Resume Next 
     Set RngFormulas = .Cells.SpecialCells(xlCellTypeFormulas) 
     On Error GoTo 0 

     If Not RngFormulas Is Nothing Then 
      For Each fCell In RngFormulas 
       On Error Resume Next 
       Set DPrcd = fCell.DirectPrecedents 
       On Error GoTo 0 
       If Not DPrcd Is Nothing Then 
        For Each aCell In DPrcd 
         If IsEmpty(aCell.Value) Then 
          Debug.Print aCell.Address & " in " & _ 
          fCell.Formula & " is empty" 
         End If 
        Next 
       End If 
      Next 
     End If 
    End With 
End Sub 

當您運行該宏時,您將在即時窗口中看到輸出。

快照

enter image description here

0

嘗試 = IF(COUNTA(A1:A3), 「OK」, 「錯誤」)找到空白的一個數組,如果通過了,那麼你可以做任何其他需要做的事情。

0

我想在我的大電子表格的每個頁面上運行公式>錯誤檢查,但我也找不到任何VBA代碼來執行它。

我想出的最好的方法就是使用Goto> Special選擇錯誤,它需要錯誤處理來忽略沒有發現錯誤時發生的錯誤(並且它不適用於#N/A常量)。

Dim r As Range 

On Error Resume Next 
Set r = ActiveCell.SpecialCells(xlCellTypeFormulas, xlErrors) 
If Err.Number <> 0 And Err.Number <> 1004 Then Stop 
On Error GoTo 0 
If Not r Is Nothing Then 
    If r.Count > 0 Then 
     r.Select 
    End If 
End If 

如果不是1004以外的錯誤「未發現的細胞。」發生的時候,我並沒有爲打破宏觀而照顧它。