User Defined Function In Array Formula

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,612
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have this simple UDF that takes a worksheet index number and returns the worksheet name :
VBA Code:
Function SheetFromIndex(index As Long) As String

    SheetFromIndex = ThisWorkbook.Worksheets(index).Name

End Function

The idea is to enter the following formula array in a range (say range A1:A3 ) and return the corresponding worksheet names in each cell ... obviously, that doesn't work and returns #Value error
{=SheetFromIndex(ROW())}

{=SheetFromIndex(ROW(A1:A3))} didn't work either.

Thanks for your help.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi Jafaar, do you mean the below?

Book1
A
1Sheet1
2Sheet2
3Sheet3
Sheet3
Cell Formulas
RangeFormula
A1:A3A1=SheetFromIndex(ROWS($A$1:$A1))
 
Upvote 0
Hi, I think your UDF would need changing to do that, something like this for example.

VBA Code:
Function SheetFromIndex(index As Variant) As Variant
Dim i As Long, j As Long, v() As String
If VarType(index) = vbArray + vbVariant Then
    j = UBound(index)
    ReDim v(1 To j)
    For i = 1 To j
        v(i) = ThisWorkbook.Worksheets(i).Name
    Next i
    SheetFromIndex = IIf(Application.Caller.Rows.Count > 1, Application.Transpose(v), v)
Else
    SheetFromIndex = ThisWorkbook.Worksheets(index).Name
End If
End Function

Cell Formulas
RangeFormula
A1:A3A1=SheetFromIndex(ROW(A1:A3))
B1:B3B1=SheetFromIndex(ROW())
C1C1=SheetFromIndex(2)
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Thanks FormR,

That will sure work but the reason I wanted this in the first place was to avoid having to iterate the worksheets in the vba UDF .

I am looking at solving this with the UDF as is in my initial post (ie: without looping) and entered in the worksheet range as an array formula.

Regards.
 
Upvote 0
Hi Jaafar, I'm not sure you can to be honest, but maybe someone else knows for sure.
 
Upvote 0
the reason I wanted this in the first place was to avoid having to iterate the worksheets in the vba UDF .

Hi Jaafar, just for clarity, in these examples, the UDF is not iterating though the worksheets, rather it's iterating through the cells the function has been entered in, each time processing the relative row number in the index parameter.
 
Upvote 0
Hi Jaafar

Do you know of a cunning way to convert a collection to an array without looping through each item in the collection?
 
Upvote 0
FWIW, for this particular purpose, the XLM function GET.WORKBOOK(1) would be simpler, as it returns an array of sheet names.
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
Latest member
oculus

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