Pass a Range object into your function and look at the value:
Public Function WhatIsTheMatrix(SourceRange as Excel.Range) As Variant
Dim arrSource as Variant
Dim arrOutPut as Variant
Dim i As LongDim j As Long
arrSource = SourceRange.Value2
i = Ubound(arrSource, 1)
j = Ubound(arrSource, 2)
' The lower bounds of an array obtained from a range are always 1
' code
' more code
' even more code
WhatIsTheMatrix = arrOutPut
End Function
Now you have two problems:
Your original problem, which is solved by inspecting i and j
A special case, in which the value of a single-cell range isn't an array variant
So you'll need this:
If SourceRange.Cells.Count = 1 Then
Redim arrSource(1 to 1, 1 To 1)
arrSource(1,1) = SourceRange.Value2
Else
arrSource = SourceRange.Value2
End If
Any other Business:
There's an unrecognized assumption, namely: you thought you could pass in the current Application.Selection and it will always be a range. It can also be a control, or a chart, or a chart series...
...And no, you can't rely on this raising a runtime error for the type mismatch. Yes, you've invoked Option Explicit and yes, you typed SourceRange as Excel.Range
and your Mother, your Parish Priest and a Justice of the Peace watched you do it; try sacrificing something on a sinister-looking rock after midnight, and maybe the VBA runtime will believe you.
So you also need this:
If Not TypeOf SourceRange Is Excel.Range Then
' Raise an error or Exit Sub
End If
I think that's all the surprises. Unless you're into exotic stuff and awkward questions like "Is this range calculated?"
dynamically selected in the spreadsheet before the function is called.
– Siddharth Rout Aug 19 '12 at 18:30x = UBound(MyArray, 2)
– Reafidy Aug 20 '12 at 6:55