User-defined function that returns an array doesn't work in a named table

chx77

New Member
Joined
Sep 15, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I made a user-defined function in vba that returns an array. It works fine in normal spreadsheets but failed when it is applied to a named table with error "#SPILL!" appears in the first cell. I'm sure there is no other content in the spill range. If I turn the normal spreadsheet range that the UDF works fine into a table structure the function became failed too with the "#SPILL!" error. I'm using Excel 365.

Here is a simple UDF to try (attached):

Public Function AAA(x as Integer) as Variant
Dim z(0 to 1) as Integer
z(0)=x +1
z(1)=x +2
AAA=z
End Function

Thanks for any advice.

chx77
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
sorry Just released I can't attach
test.jpg
an excel file. Here is any image
 
Upvote 0
I am afraid that formulas that produce an array are just not going to work inside a table structure.

refer Microsoft Doc: Dynamic array formulas and spilled array behavior

Key points

  • Spilled array formulas are not supported in Excel tables themselves, so you should place them in the grid outside of the Table. Tables are best suited to holding rows and columns of independent data.
 
Upvote 0
You can't use a SPILL function directly in a table, but you can use INDEX to get a specific element from the array and put it in a table. Here are a few examples using your sample UDF:

Book1 (version 1).xlsb
ABCDEF
1CodeFunctionValueF(v,1)F(v,2)
2A6123
3B7234
4C#REF!345
5D#REF!456
6E#REF!567
7
Sheet27
Cell Formulas
RangeFormula
E2:E6E2=INDEX(aaa([@Value]),1)
F2:F6F2=INDEX(aaa([@Value]),COLUMN([F(v,2)])-COLUMN([Value]))
B2:B6B2=INDEX(aaa(5),ROW([@Function])-ROW(INDEX([Function],1))+1)
 
Upvote 0
Hi Eric W
That's magical. Thank you for such an ingenious idea.

Also thanks Alex for the information.
Cheers
chx77
 
Upvote 0

Forum statistics

Threads
1,223,941
Messages
6,175,535
Members
452,652
Latest member
eduedu

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