เรื่องของ Null vs Empty ใน Excel VBA

Empty ใช้เปรียบเทียบค่า Variable เมื่อมีการประกาศตัวแปรและยังไม่ถูก Set ค่าใดๆ
"" ใช้เปรียบเทียบ Variable ถูก set ค่าเป็น ""
Null ใช้เปรียบเทียบค่า่า Variable ที่ถูก set เป็น Null หรือ เป็นค่า Null ที่มาจาก MS Access


Dim num As Integer
    Dim str As String
    Dim obj As Object
   
    If str = Null Then 'False
        'Variable is not null
    End If
    If str = Empty Then 'True
        Range("J3").Value = "Zero-Length-String"
    End If
    If num = Empty Then 'True
        Range("J4").Value = "Numeric 0"
    End If
    If str = ZLS Then 'True
        Range("J5").Value = "Zero-Length-String"
    End If
    If str = "" Then
    End If
    If str = "" Then
    End If
    If str = "" Then 'True
        Range("J6").Value = "Zero-Length-String"
    End If
    If Range("J7").Value = Empty Then 'True
        Range("J7").Value = "Empty cell"
    End If
    If obj Is Nothing Then 'True
        Range("J8").Value = "Obj is unset"
    End If

Reference:

Comments