Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
How to test and output a Function (Array Formula output with Ctrl Shift-Enter in Worksheet) in Immediate Window using a test Sub?
Sub aTest()
Dim myVar As Long
myVar = Evaluate("=SUM(IF(FREQUENCY(MATCH(A2:A5,A2:A5,0),ROW(A2:A5)-ROW(A2)+1),1))")
Debug.Print myVar
End Sub
Function BINARY_SORT_FUNC(ByRef DATA_RNG As Variant, _
Optional ByRef ACOLUMN As Long = 1, _
Optional ByRef VERSION As Integer = 1)
Dim i As Long
Dim j As Long
Dim k As Long
Dim ii As Long
Dim jj As Long
Dim kk As Long
Dim SROW As Long
Dim NROWS As Long
Dim SCOLUMN As Long
Dim NCOLUMNS As Long
Dim DATA_MATRIX As Variant
Dim EXCHANGE_FLAG As Boolean
On Error GoTo ERROR_LABEL
DATA_MATRIX = DATA_RNG
If VERSION <> 1 Then: VERSION = 0 ' Descending
SROW = LBound(DATA_MATRIX, 1)
NROWS = UBound(DATA_MATRIX, 1)
SCOLUMN = LBound(DATA_MATRIX, 2)
NCOLUMNS = UBound(DATA_MATRIX, 2)
If ACOLUMN = 0 Then ACOLUMN = SCOLUMN
Do
EXCHANGE_FLAG = False
For i = SROW To NROWS Step 2
k = i + 1
If k > NROWS Then Exit For
ii = DATA_MATRIX(i, ACOLUMN)
jj = DATA_MATRIX(k, ACOLUMN)
If (ii > jj And VERSION = 1) Or _
(ii < jj And VERSION = 0) Then
'swap rows
For j = SCOLUMN To NCOLUMNS
kk = DATA_MATRIX(k, j)
DATA_MATRIX(k, j) = DATA_MATRIX(i, j)
DATA_MATRIX(i, j) = kk
Next j
EXCHANGE_FLAG = True
End If
Next i
If SROW = LBound(DATA_MATRIX, SCOLUMN) Then
SROW = LBound(DATA_MATRIX, SCOLUMN) + 1
Else
SROW = LBound(DATA_MATRIX, SCOLUMN)
End If
Loop Until EXCHANGE_FLAG = False And SROW = LBound(DATA_MATRIX, SCOLUMN)
BINARY_SORT_FUNC = DATA_MATRIX
Exit Function
ERROR_LABEL:
BINARY_SORT_FUNC = Err.Number
End Function
When you assign a range to a Variant variable, even if that range is a single column of values, VBA assigns that range to the variable as a two-dimensional array. As I said in my earlier reply, the Array function only produces a one-dimensional array so your code cannot use it (it will choke when you use 2 as the second argument to the LBound and UBound function calls). So, you need to use what I posted in my earlier message to produce the required two-dimensional array, namely, this...@Rick Rothstein; the function works on a single column of data in a sheet through in coding it a 2d coding.
Sub Test()
Dim v
v = [{5;3;4;6;1}]
Debug.Print Evaluate(BINARY_SORT_FUNC(v))
End Sub
Sub Test()
Dim v, i As Long, arr As Variant
v = [{5;3;4;6;1}]
arr = BINARY_SORT_FUNC(v)
For i = 1 To UBound(v, 1)
Debug.Print arr(i, 1)
Next i
End Sub