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!
 
Just use Variant, then you can pass either a range or an array.
It really was as simple as that huh, seems to be working just fine now.

Supposedly if i would want the function to be able to accept either of an array or range, i would need to use .Column.count and .Row.count with a conditional IF THEN for the type of input received.

Thank you for the help, VBA technicalities can be such a headache when you're used to javascript
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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