phantom1975
MrExcel MVP
- Joined
- Jun 3, 2002
- Messages
- 3,962
I've read the help files and can't really digest what I'm reading. What's the difference between these?
phantom1975 said:I've read the help files and can't really digest what I'm reading. What's the difference between these?
Sub WhatsTheDifference()
Dim varMyValue As Variant, bNullCheck As Boolean, msg As String
Dim bEmptyCheck As Boolean
'** Null
'A value indicating that a variable contains no valid data. Null is the result of an
'explicit assignment of Null to a variable or any operation between expressions that
'contain Null.
'** Empty
'Indicates that no beginning value has been assigned to a Variant variable. An Empty
'variable is represented as 0 in a numeric context or a zero-length string ("") in a
'string context.
'part one - no value yet assigned to varMyValue, ie it is empty (uninitialised)
bNullCheck = IsNull(varMyValue)
bEmptyCheck = IsEmpty(varMyValue)
msg = msg & "Variable data := " & varMyValue & vbCrLf & _
"Null status := " & bNullCheck & vbCrLf _
& "Empty status := " & bEmptyCheck
MsgBox prompt:=msg, Title:="Test status"
'part two - value assigned to varMyValue, so no longer empty
msg = ""
varMyValue = 123
bNullCheck = IsNull(varMyValue)
bEmptyCheck = IsEmpty(varMyValue)
msg = msg & "Variable data := " & varMyValue & vbCrLf & _
"Null status := " & bNullCheck & vbCrLf _
& "Empty status := " & bEmptyCheck
MsgBox prompt:=msg, Title:="Test status"
'part three - null status assigned to varMyValue, no longer empty but no valid data
msg = ""
varMyValue = Null
bNullCheck = IsNull(varMyValue)
bEmptyCheck = IsEmpty(varMyValue)
msg = msg & "Variable data := " & varMyValue & vbCrLf & _
"Null status := " & bNullCheck & vbCrLf _
& "Empty status := " & bEmptyCheck
MsgBox prompt:=msg, Title:="Test status"
'part four - empty status assigned to varMyValue
'note that this is not the same, in this case, as using "" (try it for yourself!)
msg = ""
varMyValue = Empty
bNullCheck = IsNull(varMyValue)
bEmptyCheck = IsEmpty(varMyValue)
msg = msg & "Variable data := " & varMyValue & vbCrLf & _
"Null status := " & bNullCheck & vbCrLf _
& "Empty status := " & bEmptyCheck
MsgBox prompt:=msg, Title:="Test status"
End Sub
late to this thread, but how to identify (in vba) a cell that has "" as opposed to 123 or abc?
Sub mblank()
If Range("S7") = "" Then
MsgBox ("S7 is blank")
End If
End Sub