How to output Array Formula Function (Ctrl Shift-Enter) in Immediate Window using a test Sub?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
How to test and output a Function (Array Formula output with Ctrl Shift-Enter in Worksheet) in Immediate Window using a test Sub?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Set the Cell with the formula to a parameter in your code and step to the point after the parameter is populated then either type "?" and the parameter name in the immediate window or Debug.Print parameter name in the code and step through that.
 
Upvote 0
An example - formula to count unique values in A2:A5

VBA Code:
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

M.
 
Upvote 0
If I have the following function, how do I test an array using a Sub in IW?

VBA Code:
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
 
Upvote 0
Your function requires a two-dimensional array... the Array function only produces a one-dimensional array. You can use this to produce a two-dimensional array equivalent to a single column of values...

[{5;3;4;6;1}]

The square brackets is the shortcut syntax for the Evaluate function... the curly braces are Excel's symbol for an array constant. If you wanted to create a two-dimensional array equivalent to an area of cells (for this example, 3 rows by 5 columns), try this...

[{5,8,2,4,7;6,1,3,9,4;2,4,6,8,3}]

where the commas create values along a row and the semi-colon moves down one row.
 
Upvote 0
@Rick Rothstein; the function works on a single column of data in a sheet through in coding it a 2d coding.
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...

[{5;3;4;6;1}]

I am curious, though... if your function only works on a single column of data, why are you performing this calculation in your code...

SCOLUMN = LBound(DATA_MATRIX, 2)
NCOLUMNS = UBound(DATA_MATRIX, 2)

Don't you already know both of those values will be equal to 1 for ranges and properly created arrays?
 
Upvote 0
VBA Code:
Sub Test()

Dim v

v = [{5;3;4;6;1}]

Debug.Print Evaluate(BINARY_SORT_FUNC(v))

End Sub

Outputs only 1.
 
Upvote 0
This worked for me

VBA Code:
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

M.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top