I love the power of array formulas in spreadsheets, however my question is:
Can I utilise this same power in a pure VBA User-defined Function?
I have had a good look but can't seem to find how to do it?
In my current use case, I want to do an INDEX/MATCH multi-value lookup. This screen shot shows a very simplified version of my actual spreadsheet:
The magic I am trying to emulate happens in the shaded part of the formula in the above image: "A2:A22&B2:B22" which takes the two arrays and returns a single array with each item equal to the concatenation of the corresponding item in the original two arrays.
If I have two arrays of same length in VBA, I can't find how to make this same magic happen.
Note that I do NOT want to construct a string formula and place it in a spreadsheet. My goal is to do everything in VBA and just return the found value.
I know I can easily do this with For loops, but the data table can become very large (many tens of thousands of rows) and I want to make use of the lovely optimised code built into the guts of Excel rather than slow VBA loops.
I need this function to work as fast as possible as I will be using it to create other large tables with potentially thousands of uses of the function and that's why I am exploring all the options.
Basically I was hoping this simple looking code would work (but it fails):
I have a VLookup version working but it requires an extra column to be added to pre-calculate the lookup keys and I'm trying to avoid having to do that:
So, is there a way to perform these magic array formulas in pure VBA?
Note: Cross-posted here
Can I utilise this same power in a pure VBA User-defined Function?
I have had a good look but can't seem to find how to do it?
In my current use case, I want to do an INDEX/MATCH multi-value lookup. This screen shot shows a very simplified version of my actual spreadsheet:
The magic I am trying to emulate happens in the shaded part of the formula in the above image: "A2:A22&B2:B22" which takes the two arrays and returns a single array with each item equal to the concatenation of the corresponding item in the original two arrays.
If I have two arrays of same length in VBA, I can't find how to make this same magic happen.
Note that I do NOT want to construct a string formula and place it in a spreadsheet. My goal is to do everything in VBA and just return the found value.
I know I can easily do this with For loops, but the data table can become very large (many tens of thousands of rows) and I want to make use of the lovely optimised code built into the guts of Excel rather than slow VBA loops.
I need this function to work as fast as possible as I will be using it to create other large tables with potentially thousands of uses of the function and that's why I am exploring all the options.
Basically I was hoping this simple looking code would work (but it fails):
Code:
Function LookupDepartmentName(department As String, firstName As String)
'Trying to convert {=INDEX(C2:C22,MATCH(F3&F4,A2:A22&B2:B22,0))} to VBA...
LookupDepartmentName = WorksheetFunction.Index(ActiveSheet.Range("C2:C22"), WorksheetFunction.Match(department & firstName, ActiveSheet.Range("A2:A22") & ActiveSheet.Range("B2:B22"), 0))
End Function
I have a VLookup version working but it requires an extra column to be added to pre-calculate the lookup keys and I'm trying to avoid having to do that:
Code:
Function LookupDepartmentName(department As String, firstName As String)
'NOTE: This assumes a different spreadsheet from that shown above
' It would need a pre-calculated column of lookup keys inserted before column A
LookupDepartmentName = WorksheetFunction.VLookup(department & firstName, ActiveSheet.Range("A2:D22"), 4, False)
End Function
So, is there a way to perform these magic array formulas in pure VBA?
Note: Cross-posted here