Create UDF from Macro

J

JTF

Hi all

I have the following VB code

Sub ErrorCheck2()
'Error checking for when H1=1
'Error message to display in cell B50
Dim d As Range
If Range("h1") = 1 Then
Range("b50") = ""
For Each d In Range("F19:F38")
If d.Text <> "" And d.Text > "119999" Then
Range("B50") = "ERROR"
Exit Sub
End If
Next
End If
End Sub

I wanted to make this a UDF - but it is not visible when I display the
list of UDF's. This is all new to me, I am learning as I go, and from
what I can see - I need to tell Excel somehow that this is a function
by putting FUNCTION in the code. But I am not sure what syntax I need.


Can someone give me some guidance..(I guess the short question is - I
want to be able to place this macro in a cell as a formula so it will
run automatically). Thanks
 
J

Jerry W. Lewis

A UDF is defined by Function ... End Function instead of Sub ... End Sub

A UDF returns a value, i.e. includes a line like
ErrorCheck2 = ...

When called from a worksheet formula, a UDF only returns that value and
cannot modify its environment in any way (in particular, it cannot
change the value in some cell other than the one that called the function).

Jerry
 

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

Top