User Defined Function with Array argument

adre

New Member
Joined
Feb 10, 2013
Messages
6
I am trying to create a User Defined Function with Array arguments that I want to use in an Array formula. After some debugging, I isolated the problem to this.

VBA Code :
Public Function MyFn(MyRng As Range) As Variant
MyFn = 2
End Function


In the worksheet,
array formula {=MyFn($H$12:$H$15)} gives the result 2.00 - Good!
array formula {=MyFn((J12:J15="GT")*$H$12:$H$15)} gives the result #VALUE! - Why?

I checked with Evaluate Formula and saw that the Formula evaluates to: MyFN({0;-16384.752816512;0;0})
So, I check the array formula {=MyFn({0;16;0;0})} and sure enough it gives the result #VALUE!

Can anyone figure out what exactly is wrong with the array formula {=MyFn((J12:J15="GT")*$H$12:$H$15)} when applied to the UDF code above? TIA.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Your UDF is written to take a range argument, not an array.
 
Upvote 0
Thanks for your response.
I tried "Public Function MyFn(MyRng() As Single) As Variant" as the first line of my UDF, but not even {=MyFn($H$12:$H$15)} works in that case. I would appreciate if you point out the right syntax.
 
Upvote 0
It would appear that a Variant would be the most suitable parameter type here.
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,306
Members
449,095
Latest member
Chestertim

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