Hi all,
Trying to learn some VBA. One code I'm trying to write is a table lookup function. The table has row and heading titles. I want arguments of the function to be a row title, column title, and table range, and the returned value to be the contents of the cell corresponding to that row and column.
Here's what I've tried. It results in a #VALUE error:
Function Getdata(row_name As String, col_name As String, table_array As range) As Double
Dim row_index As Integer
Dim col_index As Integer
row_index = WorksheetFunction.Match(row_name, range(table_array).Rows(1), 0)
col_index = WorksheetFunction.Match(col_name, range(table_array).Columns(1), 0)
Getdata = WorksheetFunction.Index(table_array, row_index, col_index)
End Function
This is just using the in-built XL functions MATCH and INDEX, and incorporating them into a single function, GETDATA. When setting row_index and col_index, I try to lookup just the first row or column of the table using range(table_array).Rows(1), and when a I add a breakpoint on this line, mousing over this part displays the message "<Method 'Range' of object '_Global' failed>". I am very confused with ranges and how they work... they seem to be both an object and a property, and according to this message it's trying to be a method as well. Can anyone help me out?