Custom Functions

djt76010

Board Regular
Joined
Feb 23, 2007
Messages
109
I am writing some custom functions to be saved in an Add-In file, but am having trouble saving named ranges. I would like named ranges in the Add-In file to be available in other spreadsheets. Is there a way to do this? Or is there a way to take a range from Excel and save it into a globally available named range using VBA?
 
Sorry, the code I have written is recursive and lengthy and so I just posted a simplified version. I figured out that by defaulting the value of an Optional variable you can test the default value

Code:
Function MyFunction(Age, Optional Table As String = "missing")

    Dim MyRANGE As Range
    
    If Table = "missing" Then
        MyFunction = "Table Missing"
    Else   
        Set MyRANGE = ThisWorkbook.Names(Table).RefersToRange
        MyFunction = Application.VLookup(Age, MyRANGE, 2, False)
    End If

End Function

but if the variable is not Optional the default variable assignment is telling me that there is a compile error because it is expecting a list separator or ) instead of the =

Code:
Function MyFunction(Age, Table As String = "missing")

    Dim MyRANGE As Range
    
    If Table = "missing" Then
        MyFunction = "Table Missing"
    Else   
        Set MyRANGE = ThisWorkbook.Names(Table).RefersToRange
        MyFunction = Application.VLookup(Age, MyRANGE, 2, False)
    End If

End Function
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I don't see anything wrong with the first version. (It wouldn't make sense to have a default value for a variable that is not optional!)
 
Upvote 0
When you add the "As String" declaration to the variable the "IsMissing" test stops working. How do I test for missing "As String" variables that are not optional?
 
Upvote 0
If you fail to give a non-optional argument, the calling statement (myVar = myFunction) will give a "Argument not optional" error.

If you have an optional string argument and the argument is omitted, that argument will be set to the default; either the default from the function declaration statement or vbNullString (if no such default is specified).
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top