I have a range and want to find the intersection of the column name and row name. Something like =ARRAYVALUE(“Employee Data”, “Weekly Salary”, “Joe Bloggs”) where ARRAYVALUE is the name of the VB function, Employee Data is the name of the range, “Weeky Salary” the name of the column, and “Joe Bloggs” is a row. It would give me how much Joe Bloggs earns per week from the range (e.g. $5000).
An example of how to do this is given in the help, but it’s rather complex and not very readable, so I’m wondering if someone has written a VB function to simplify it all up in the manner of the example above.
It’s certainly possible to do this with a VBA function, although do you just need to use this in a particular workbook, or do you want it to be accessible from multiple workbooks? The former is easier. The latter requires either a fully qualified reference in the formula, something like (but probably not exactly like, I’d have to look it up):
Here is the VBA code to do this within the same workbook. It uses the same solution that others have probably noted but encapsulates it in VBA. Two things to note:
This code must be placed in a module. That is true for any user-defined function to work.
The first argument is not a string giving the name of the range, it’s the name of the range itself. So in your example above, you would instead omit the quotes like this:
On edit I note that Employee Data is an illegal range name because it contains a space; you would have to use something like EmployeeData.
Here is the code, I have tested this. It will work no matter where your range is (that is, it does not have to start in A1):
Option Explicit
Public Function ARRAYINDEX(indexRange As Range, columnValue As Variant, rowValue As Variant) As Variant
Dim row_num As Long
Dim column_num As Long
With indexRange.Cells(1, 1)
row_num = WorksheetFunction.Match(rowValue, .EntireColumn, 0) - .Row + 1
column_num = WorksheetFunction.Match(columnValue, .EntireRow, 0) - .Column + 1
ARRAYINDEX = WorksheetFunction.INDEX(indexRange, row_num, column_num)
End With
End Function
If you are not familiar with VBA or creating user-defined functions, write back.
Thanks to all. As I noted in my OP, the problem was not ‘how to do it exactly’, because they already detail that, but ‘how to do it legibly’, and CookingWithGas’s function does it admirably.