Nothing の判定

オブジェクトが Nothing かを判定します。
オブジェクトが Nothing の場合、エラーとなるので重要な判定処理です。

【使い方】

    If obj Is Nothing Then
        ' Nothing の場合
    End If
    If Not obj Is Nothing Then
' Nothingの場合の処理
End If

【例1】Nothing の場合

Sub Sample1(ws As Worksheet)

    ' Nothing の場合
    
    If ws Is Nothing Then
        ' Nothing の場合、アクティブなシートを設定する
        
        Set ws = ActiveSheet
   
    End If

End Sub

【例2】Nothing ではない場合

Sub Sample2(ws As Worksheet)

    ' Nothing ではない場合
    
    If Not ws Is Nothing Then
        ' Nothingの場合の処理
    
    Else
        ' Nothing の場合、処理終了
        MsgBox "ワークシートが設定されていません"
        Exit Sub
    End If

End Sub

コメント