How to refer to a blank/null value?

D

desmondleow

I have the following If Else statement:

If CDbl(myString) <> "" Then
cells(n,9).Value = CDbl(myString)
End If


However, it doesn't work. MyString is declared as a String and it is
copying a field from another application. How can I check that
myString is empty/null?
 
B

Bob Phillips

Desmond,

Why cast it to a double and then test as a string?

Various tests you can try

If IsEmty(myString) Then

If myString = "" Then

If Len(myString) = 0 Then

In your example, this should wotk

If myString <> "" Then
Cells(n, 9).Value = CDbl(myString)
End If

but you might want to test myString for a numeric value before casting to a
double.




Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
D

David Coleman

Hi there

The problem is almost certainly due to the CDbl(myString) - this forces
Excel to convert myString to "a data type that holds double-precision
floating-point numbers" - try removing both references to CDbl from the code
and see what happens...

However, I'm surprised that it allows the code to run at all - testing
CDbl(myString) with myString set to "Fred" results in a type mis-match
error - are you convinced that myString really is a string?

Regards

David

***** MY TEST CODE *****
***** set mystring to "" or "fred" and you should see the results that
you're expecting....

Sub macro1()

mystring = ""

If ((mystring) <> "") Then MsgBox ("Non-empty string") Else MsgBox ("empty
string")

End Sub
 
D

desmondleow

Is there anyway to test myString for a numeric value before I cast it to
a double?
 
D

David Coleman

IsNumber (myString) should do the trick (IsText exists if you want to check
the other way)

Regards

David
 
Top