ExcelDNA: Parameter Limit for Excel Functions Using a Variable Parameter Array (params and ParamArray)
Found out something interesting recently when trying to build a custom Excel function with a lot (!) of parameters...
Implementing a custom Excel function using ExcelDNA is fairly straight forward. However, when trying to use a parameter array as a parameter, you must complete some prerequisites.
First, you must include a reference to ExcelDna.Registration, register the parameter array, and register the rest of the functions. This can be placed anywhere in code assuming it's run once at start up (either bound to some Excel AutoOpen event or your application's main class constructor, for instance)
ExcelRegistration.GetExcelFunctions().ProcessParamsRegistrations().RegisterFunctions();
Second, you must include the decorator IsMacroType:=True for it to be picked up by Excel:
<ExcelFunction(Name:="TestFunction", Description:="Custom Excel Function", IsThreadSafe:=False, IsMacroType:=True)> Public Shared Function TestFunction(ParamArray params As Object()) As Object Return "Finished!" End Function
However, this isn't the full story. When searching Google for the maximum number of allowable parameters for an Excel function, it's pretty commonly reported as 255 -- even ChatGPT tells me so! And yet if you try to pass more than 125 parameters, TestFunction will fail to trigger and return a #VAL! So what's going on?
As it turns out, ExcelDNA doesn't support more than 125 parameters for a custom Excel function leveraging a variable parameter array (such as params or ParamArray, depending on language). It's actually documented in the library's XML but you can go straight to the source to see why:
int maxArguments; if (ExcelDnaUtil.ExcelVersion >= 12.0) { maxArguments = 125; // Constrained by 255 char registration string, take off 3 type chars, use up to 2 chars per param (before we start doing object...) (& also return) // CONSIDER: Might improve this if we generate the delegate based on the max length... } else { maxArguments = 29; // Or maybe 30? }
The story is almost at its close -- after discovering that a parameter array won't work (as I need to pass more than 125 parameters), I implemented an approach using optional, manually added, parameters:
<ExcelFunction(Name:="TestFunction", Description:="Custom Excel Function", IsThreadSafe:=False, IsMacroType:=True)> Public Shared Function TestFunction(Optional p001 As Object = Nothing, ..., Optional p255 As Object = Nothing) As Object Return "Finished!" End Function
At this point, Excel wouldn't even load my XLL. What gives?! After some playing around, I found the formula was only recognized with 250 parameters, not 255.
So what did we learn about ExcelDNA custom functions today?
The maximum number of parameters for a custom function is 250.
The maximum number of parameters for a custom function using a variable parameter array (params or ParamArray) is 125.
Sure wish I knew this at the start of the day!
.svg.png)
Comments
Post a Comment