UDF help

  • Thread starter Thread starter John C
  • Start date Start date
J

John C

What are some good references on creating UDF's?

While my real need is far more complex than I list here, I will give a basic
basic situation.
A2: =IF(A1="","",A1)

I would prefer something more like
A2: =NullCell(A1)

Thank you.
 
It is quite simple.

The value is returned as the function name

Function NullCell()

'some code
NullCell = somevalue
End Function

If you want to pass a value into the function you just declare an argument

Function NullCell(ByRef rng As Range)

'do something to rng
NullCell = somevalue
End Function
 
What are some good references on creating UDF's?

While my real need is far more complex than I list here, I will give a basic
basic situation.
A2: =IF(A1="","",A1)

I would prefer something more like
A2: =NullCell(A1)

Thank you.

For the basic situation you can try the following UDF:

Function NullCell(r As Range) As Variant
If r.Value = "" Then
NullCell = ""
Else
NullCell = r.Value
End If
End Function

Hope this helps / Lars-Åke
 
Not quite as simple as that. I wish it were.
If A1 is either blank or empty, I want the cell where the NullCell function
is to appear empty.
If A1 is blank due to formula (i.e.: =""), then the NullCell function works,
but if there is NO data in there, I get an answer of 0.
 
I come up with #VALUE! errors, no matter when tested. static text, static
number, formulated text, formulated number, empty cell by formula, empty cell
because it's just empty.
 
For the basic situation you can try the following UDF:

Function NullCell(r As Range) As Variant
If r.Value = "" Then
NullCell = ""
Else
NullCell = r.Value
End If
End Function

Hope this helps / Lars-Åke

Or why not just like this:

Function NullCell(r As Range) As Variant
NullCell = r.Value
End Function
 
Or why not just like this:

Function NullCell(r As Range) As Variant
NullCell = r.Value
End Function

No, that did not work. back to my first proposal:

Function NullCell(r As Range) As Variant
If r.Value = "" Then
NullCell = ""
Else
NullCell = r.Value
End If
End Function
 
Try something like:

Function NullCell(rng As Range) As Variant
If rng.Value = "" Then
NullCell = ""
Exit Function
End If
NullCell = rng.Value
End Function


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
#VALUE will be the result e.g. if you have not the same spelling of r
in (r as Range) and in r.Value

To me the function work as expected for all cases you mention.

Lars-Åke
 
Thank you very much.
--
John C


Sandy Mann said:
Try something like:

Function NullCell(rng As Range) As Variant
If rng.Value = "" Then
NullCell = ""
Exit Function
End If
NullCell = rng.Value
End Function


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 
You're very welcome.

--

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

(e-mail address removed)
Replace @mailinator.com with @tiscali.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top