2012-07-09 24 views
5

voglio verificare un valore #N/A in Excel con VBA. Così, dopo alcune ricerche, ho fatto questo codice:controllare un valore "# N/A" in VBA in una gamma

Set MyTab = Range(name) 
If (Not IsEmpty(MyTab.value)) And (MyTab(i, j).value <> CVErr(xlErrNA)) Then 
    Call BuildRequest(False, id, MyTab, i, j) 
End If 

Ma quando si passò MyTab(i, j).value <> CVErr(xlErrNA) ho un errore 13(type error) e non trovo il motivo.

Chiunque può aiutarmi?

risposta

9

Innanzitutto è necessario verificare che la cellula contiene un errore:

If IsError(MyTab(i, j).Value) Then 
    If MyTab(i, j).Value <> CVErr(xlErrNA) Then 

A meno che non si vuole conoscere il tipo di errore (# N/A, # DIV/0 !, ecc), si potrebbe anche sostituire il test con:

If (Not IsEmpty(MyTab.value)) And (Not IsError(MyTab(i, j).value)) Then 

Se avete bisogno di controllare il tipo di errore, si può scrivere:

Dim shouldBuildRequest As Boolean 

shouldBuildRequest = Not IsEmpty(MyTab.value) 
If IsError(MyTab(i, j).Value) Then 
    shouldBuildRequest = shouldBuildRequest AND (MyTab(i, j).Value <> CVErr(xlErrNA)) 
End If 

If shouldBuildRequest Then 
    Call BuildRequest(False, id, MyTab, i, j) 
End If 
+0

+ 1 Bello :) –

+0

Grazie per questo :). Tu a Siddharth Rout –

4

Un altro w ay per verificare l'errore

If CVErr(MyTab(i, j).Value) = CVErr(xlErrNA) 
+1

Pfffff .... trovi sempre un modo migliore! ;-) – assylias

+0

Sto provando a fare un controllo degli errori simile per le celle unite ("B6: D6"), sto usando il codice qui sotto e ho trovato un errore. Se CVErr (ws.Range ("B6"). MergeArea.Cells (1, 1) .Value) = CVErr (xlErrNA) Quindi – Isu