I have a Variant type 2D array. The size and values ββthat fill the array are generated based on the data in the sheet. For this array, additional processing is required, the primary one is the interpolation of several values. I use this interpolation function (I know about the equivalent excel functions, but the design was not chosen for their use). The problem I am facing is that a Range object is required for the interpolation function.
I have already tried modifying the function to use the Variant argument ( r as Variant ). The next line nR = r.Rows.Count can be replaced with nR = Ubound(r) . Although this works, I would also like to use this function, as a rule, in any worksheet, and not change the function in any way.
Sub DTOP() Dim term_ref() As Variant ' snip ' ReDim term_ref(1 To zeroRange.count, 1 To 2) ' values added to term_ref ' ' need to interpolate x1 for calculated y1 ' x1 = Common.Linterp(term_ref, y1) End Sub
Interpolation function
Function Linterp(r As Range, x As Double) As Double Dim lR As Long, l1 As Long, l2 As Long Dim nR As Long nR = r.Rows.Count ' snipped for brevity ' End Function
How can I convert my array in memory to a range so that it can be used for the interpolation function? (without output to the worksheet)
Answer
In short, the answer is you cannot. The Range object must reference the worksheet.
The modified interpolation function checks the TypeName argument and sets the value nR accordingly. Not the most beautiful solution.
As a note, the VarType function VarType out to be useless in this situation, since both VarType(Variant()) and VarType(Range) returned the same value (i.e. vbArray) and could not be used to disambiguate the array from the range
Function Linterp(r As Variant, x As Variant) As Double Dim lR As Long, l1 As Long, l2 As Long Dim nR As Long Dim inputType As String inputType = TypeName(r) ' Update based on comment from jtolle If TypeOf r Is Range Then nR = r.Rows.Count Else nR = UBound(r) - LBound(r) 'r.Rows.Count End If ' .... End Function