0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Excel VBA】IsError関数|エラーが発生したかどうか判定する方法と注意点

0
Last updated at Posted at 2025-08-04

この記事ではIsError関数の使い方と注意点について解説します。
他のよく使うVBA関数一覧はこちら。

IsError関数の使い方と注意点

IsError関数は、値がエラーかどうかを判定する関数です。  
VLOOKUPや計算結果が#N/A#DIV/0!などのエラーになる可能性がある場合に、事前にチェックする目的でよく使います。

構文

IsError()
  • : チェック対象の値または式(Variant型)
  • 戻り値 : 引数がエラーなら(#DIV/0!#N/Aなど)True、それ以外はFalse(Boolean型)

使用例

計算結果がエラーかどうかを判定する

Sub Sample()
    Dim val As Variant
    val = CVErr(xlErrDiv0)  ' ゼロ除算のエラーを明示的に作る

    If IsError(val) Then
        Debug.Print "エラーです"
    Else
        Debug.Print "正常です"
    End If
End Sub

▶ 出力結果

エラーです

xlErrDiv0は0除算エラー(#DIV/0!)を表す定数です。

CVErr関数は引数で指定されたエラー番号をエラー値として返す関数です。
セルに#DIV/0!などのエラーを擬似的に入れたいときに使います。

VLOOKUPの戻り値がエラーかどうか判定する

Sub Sample()
    Dim result As Variant
    result = Application.VLookup("ZZZ", Range("A1:B10"), 2, False)
    ' A1:B10内に「ZZZ」が見つからないと、VLOOKUPがエラー(#N/A)を返す

    If IsError(result) Then
        Debug.Print "該当データなし"
    Else
        Debug.Print result
    End If
End Sub

▶ 出力結果(該当データ(ZZZ)がない場合)

該当データなし

⚠️注意

計算式に直接エラーが出ると、IsErrorに渡す前に実行時エラーで処理が停止します

以下のように、ゼロ除算などを直接IsErrorに渡すと、エラーが発生して処理が停止します。

Sub Sample()
    Dim result As Boolean
    result = IsError(1 / 0) 
End Sub

▶ 出力結果
error_11.png

On Error Resume Nextで回避し、IsErrorでチェックする

Sub Sample()
    Dim val As Variant
    On Error Resume Next
    val = 1 / 0
    On Error GoTo 0

    If IsError(val) Then
        Debug.Print "ゼロ除算エラーが発生しました"
    End If
End Sub

▶ 出力結果

ゼロ除算エラーが発生しました

On Error Resume Nextは、エラーが発生しても処理を止めずに次の行へ進める命令です。
エラー値は変数に代入でき、IsError関数で検出できます。

IsErrorで判定できる主なエラー一覧

エラー名 内容
#DIV/0! ゼロによる除算
#N/A 検索対象が見つからない
#VALUE! 無効なデータ型
#REF! 無効なセル参照
#NAME? 未定義の関数や変数
#NUM! 無効な数値演算

関連するVBA関数

  • IsNumeric関数
  • IsEmpty関数
  • CVErr関数

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?