เรื่องของ Null vs Empty ใน Excel VBA
Empty ใช้เปรียบเทียบค่า Variable เมื่อมีการประกาศตัวแปรและยังไม่ถูก Set ค่าใดๆ
"" ใช้เปรียบเทียบ Variable ถูก set ค่าเป็น ""
Null ใช้เปรียบเทียบค่า่า Variable ที่ถูก set เป็น Null หรือ เป็นค่า Null ที่มาจาก MS Access
Reference:
"" ใช้เปรียบเทียบ 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:
- http://developer.rhino3d.com/guides/rhinoscript/nothing-empty-null/
- https://stackoverflow.com/questions/2631678/vba-check-if-object-is-set
- http://allenbrowne.com/vba-NothingEmpty.html
- http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=267:excel-vba-empty-zls-null-nothing-missing&catid=79&Itemid=475
Comments
Post a Comment