Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Passing null to a function

Hi,
I am trying to create a function to deal with nulls, but I'm enconntering an
error (invalid use of null) by simply passing null to a function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing null to a function

Andy,

Null is a special subtype of Variant type variables. An empty
string is NOT a null. IsNull tests only Variant type variables.
To test for an empty string, use code like

If S = "" Then
' or
If Len(S) = 0 Then


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andy" wrote in message
...
Hi,
I am trying to create a function to deal with nulls, but I'm
enconntering an
error (invalid use of null) by simply passing null to a
function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Passing null to a function

Many thanks Chip.
I obviously need to use variants.
Regards,
Andy

"Chip Pearson" wrote:

Andy,

Null is a special subtype of Variant type variables. An empty
string is NOT a null. IsNull tests only Variant type variables.
To test for an empty string, use code like

If S = "" Then
' or
If Len(S) = 0 Then


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andy" wrote in message
...
Hi,
I am trying to create a function to deal with nulls, but I'm
enconntering an
error (invalid use of null) by simply passing null to a
function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing null to a function

I obviously need to use variants.

An uninitialized Variant is not Null, it is Empty, which you can
test with the IsEmpty function. E.g.,

Dim V As Variant
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

A Variant is Null only when you specifically assign Null to it,
and a Variant set to Null is NOT Empty.

Dim V As Variant
V = Null
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

I think you need to rethink your usage of Null as opposed to
Empty.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Andy" wrote in message
...
Many thanks Chip.
I obviously need to use variants.
Regards,
Andy

"Chip Pearson" wrote:

Andy,

Null is a special subtype of Variant type variables. An empty
string is NOT a null. IsNull tests only Variant type
variables.
To test for an empty string, use code like

If S = "" Then
' or
If Len(S) = 0 Then


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andy" wrote in message
...
Hi,
I am trying to create a function to deal with nulls, but I'm
enconntering an
error (invalid use of null) by simply passing null to a
function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing null to a function

I should have added that a Variant containing an empty string is
neither Null nor Empty.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Chip Pearson" wrote in message
...
I obviously need to use variants.


An uninitialized Variant is not Null, it is Empty, which you
can test with the IsEmpty function. E.g.,

Dim V As Variant
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

A Variant is Null only when you specifically assign Null to it,
and a Variant set to Null is NOT Empty.

Dim V As Variant
V = Null
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

I think you need to rethink your usage of Null as opposed to
Empty.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Andy" wrote in message
...
Many thanks Chip.
I obviously need to use variants.
Regards,
Andy

"Chip Pearson" wrote:

Andy,

Null is a special subtype of Variant type variables. An empty
string is NOT a null. IsNull tests only Variant type
variables.
To test for an empty string, use code like

If S = "" Then
' or
If Len(S) = 0 Then


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andy" wrote in message
...
Hi,
I am trying to create a function to deal with nulls, but
I'm
enconntering an
error (invalid use of null) by simply passing null to a
function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Passing null to a function

Many thanks Chip.
In my instance the items in question can be nulls, so variants would seem to
be the way to go.
Regards,
Andy

"Chip Pearson" wrote:

I should have added that a Variant containing an empty string is
neither Null nor Empty.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Chip Pearson" wrote in message
...
I obviously need to use variants.


An uninitialized Variant is not Null, it is Empty, which you
can test with the IsEmpty function. E.g.,

Dim V As Variant
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

A Variant is Null only when you specifically assign Null to it,
and a Variant set to Null is NOT Empty.

Dim V As Variant
V = Null
Debug.Print "IsEmpty: " & IsEmpty(V)
Debug.Print "IsNull: " & IsNull(V)

I think you need to rethink your usage of Null as opposed to
Empty.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com




"Andy" wrote in message
...
Many thanks Chip.
I obviously need to use variants.
Regards,
Andy

"Chip Pearson" wrote:

Andy,

Null is a special subtype of Variant type variables. An empty
string is NOT a null. IsNull tests only Variant type
variables.
To test for an empty string, use code like

If S = "" Then
' or
If Len(S) = 0 Then


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Andy" wrote in message
...
Hi,
I am trying to create a function to deal with nulls, but
I'm
enconntering an
error (invalid use of null) by simply passing null to a
function.

The following code creates the issue

Sub go()
Dim sreturn As String
sreturn = nulls("hello")
MsgBox "return is :" & sreturn

sreturn = nulls(Null)
MsgBox "return is :" & sreturn
End Sub

Function nulls(instring As String) As String
If IsNull(instring) Then
nulls = ""
Else
nulls = instring
End If
End Function

Does anybody have any ideas?

Many thanks in advance
Andy








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Zero and Null Sum Function Frustrated by Averages[_2_] Excel Worksheet Functions 3 May 12th 10 11:19 PM
Passing a row to a function dch3 Excel Worksheet Functions 2 July 31st 06 12:34 PM
Passing a WorkSheet from a Function??? Mac Lingo Excel Worksheet Functions 3 June 13th 06 08:29 AM
VBA - Passing a FUNCTION as an Argument James B Excel Programming 3 February 18th 04 03:42 PM
Passing array to a function GB[_3_] Excel Programming 3 October 21st 03 09:59 AM


All times are GMT +1. The time now is 12:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"