VBA dynamic array - handling rows an columns

nicklasx

New Member
Joined
Oct 26, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello Mrexcel community,

How does VBA access columns and rows in a dynamic array/matrix? Say for instance i use the array:

Excel Formula:
=RANDARRAY(5 rows , 4 columns)

Normally, ROWS(RANDARRAY(...)) = n_rows and COLUMNS(RANDARRAY(...))=n_columns, but when using VBA, there doesnt seem to be the same
logic to handling the rows and columns, when the array does not exist "typed out" within the spreadsheet.

VBA Code:
Function COLMAX(Data_Range As Range) As Variant
    Dim TempArray() As Double, i As Long
        If Data_Range Is Nothing Then Exit Function
        With Data_Range
        ReDim TempArray(1 To .Columns.Count)
        For i = 1 To .Columns.Count
            TempArray(i) = Application.Max(.Columns(i))
        Next
    End With
        COLMAX = TempArray
End Function

If i attempt to call this function as =COLMAX(RANDARRAY(3,5)) it returns an error, as #NAME?. The same problem occurs with the function:

VBA Code:
Function COLCOUNT(Data_Range As Range) As Variant
        COLCOUNT= Data_Range.Columns.Count
End Function

So how do i instead access rows and columns of a dynamic array using VBA, if not by using .Columns and .Rows? It should be noted that i am still fairly new to using VBA in excel.

Thank you for any and all insight on how to work around this issue!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Where exactly is that function located? Is it in a standard module in the same workbook you are trying to use it in?
 
Upvote 0
Where exactly is that function located? Is it in a standard module in the same workbook you are trying to use it in?
Both the VBA functions mentioned are listed as module 1 and module 2 within the worksheet.

I am able to use both when marking an array, for example COLMAX(A1:C10) gives the three maxima of column A, B and C.

1635950086151.png
 
Upvote 0
RANDARRAY returns an array, not a Range, so your code won't work. Arrays don't have properties - you need to test the UBound of the relevant dimension - eg Ubound(array, 1) for the number of 'rows'.
 
Upvote 0
Solution
In that case I see no reason why you would get a #NAME? error. I would expect it to be a #VALUE! error.

Your udf expects a range, which is why it won't work work when you try to pass it an array from another function.
 
Upvote 0
RANDARRAY returns an array, not a Range, so your code won't work. Arrays don't have properties - you need to test the UBound of the relevant dimension - eg Ubound(array, 1) for the number of 'rows'.
Yes indeed i'm aware that the whole problem stems from arrays being treated differently from ranges, despite having similar structure.

Ive encountered Ubound and Lbound before, but i was under the impression that they treated 2d arrays as 1d vectors. I'll look into the documentation and see if i can make it work then, and Thanks!

Will this be applicable to columns as well as rows?
 
Upvote 0
In that case I see no reason why you would get a #NAME? error. I would expect it to be a #VALUE! error.

Your udf expects a range, which is why it won't work work when you try to pass it an array from another function.
Yeah i'm equally as confused as you are
 
Upvote 0
Many thanks for the help Rory!

The only problem now is that for some reason i cannot seem to load in an array as the input for my function. I do not know if it has to do with how it is declared, but i would assume as much?

VBA Code:
Function COLMAX(Data_Range1 As Range, Data_Range2 As Range) As Variant

If not "as range" then no other variable declaration seems to fit my need? As array doesn't seem to be an option. I stumbled upon this syntax as a possible replacement

VBA Code:
processArr(ParamArray Arr() As Variant) As Variant

But playing around with it only lead me to further confusion.

So, how do i properly declare that my function should take in and be executed as:

Excel Formula:
=COLMAX( RANDARRAY(2,3,1,10,TRUE) )

where that array then becomes an accessible dim variant that i can apply i.e. UBound(array,1), or simply put:

VBA Code:
Function COLMAX(Data_Range1 As Range) As Variant

COLMAX=UBound(Data_Range1,1)

End Function
 
Upvote 0
Just use Variant, then you can pass either a range or an array.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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