Sculpt

Professional Excel development

Create a custom function

leave a comment »

When you frequently use a non-standard function, it may be an idea to create a custom function at workbook start-up using a User-defined function (UDF). The IF(ISERROR) function is one example of this.

We can create a custom IF(ISERROR) function in two easy steps:

1. Implement the custom function:
___________________________________
Public Function IFISERROR(ByRef Evaluate As Variant, _
ByRef Default As Variant) As Variant
If IsError(Evaluate) Then
IFISERROR = Default ‘if an error, returns default
Else
IFISERROR = Evaluate ‘no error, evaluates function
End If

End Function
___________________________________

The function is now available to use – this is accessible from the User Defined category in Insert Function. However, this offers no guide how the function works. You can add this information be registering the UDF in step 2.

2. Register the custom UDF:
___________________________________
Sub CreateCustomFunction()

Dim strDescription As String
strDescription = “A custom IF(ISERROR) function: ” & _
vbLf & “=IF(ISERROR(function),default value,function)”
‘this text will appear in the function dialog box
Application.MacroOptions Macro:=”IFISERROR”, _
Description:=strDescription, _
Category:=9 ’9=Information category of UDF category names

End Sub
___________________________________

Run the CreateCustomFunction (you can run this from the Workbook_Open event) and your new custom function will appear in the function dialog box:

customfunction.JPG

Advertisement

Written by Austin

January 4, 2007 at 1:10 pm

Posted in functions, if(iserror), vba

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.