Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I pass an array(within an array) as an argument?

I have an array of arrays arrAggregatedArrays(1 to 8)

I can call a sub like this:

call sub(ArrNewClient)

But I get a compile error: "Type Mismatch" if I try this:

call sub(arrAggregatedArrays(1))

Why? And is there a way around it?

and why does it not recognise arrAggregatedArrays(1) as an array even though it will perform functions like UBound on it like normal?

Public arrAggregatedArrays()     As Variant      '/ Holds all the sheet-Data Arrays  

'/ Declared in a seperate module

      ReDim arrAggregatedArrays(1 To 8)
            arrAggregatedArrays(1) = arrNewClient
            arrAggregatedArrays(2) = arrExistingClient
            arrAggregatedArrays(3) = arrGroupSchemes
            arrAggregatedArrays(4) = arrOther
            arrAggregatedArrays(5) = arrMcOngoing
            arrAggregatedArrays(6) = arrJhOngoing
            arrAggregatedArrays(7) = arrAegonQuilterArc
            arrAggregatedArrays(8) = arrAscentric

      Call FilterSheetArrayForColumns(arrAggregatedArrays(1))

Public Sub FilterSheetArrayForColumns(ByRef arrCurrentArray() As Variant)

and a screenshot:

Compile Error

like image 957
Kaz Avatar asked Aug 23 '15 00:08

Kaz


People also ask

Can you pass an array as an argument?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do you pass an entire array to a function as an argument?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

How do you pass an array as an argument to a function in Javascript?

Method 1: Using the apply() method: The apply() method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.

Why Cannot arrays be passed by value to functions?

Arrays are not passed by value because arrays are essentially continuous blocks of memmory. If you had an array you wanted to pass by value, you could declare it within a structure and then access it through the structure.


1 Answers

You can create a Variant array in one of two ways:

Dim v1() As Variant
Dim v2: v2 = Array()

With the former, you receive the array as a subroutine parameter using the v1() notation, like with any other data type array in VBA. With the latter, you'll need to receive it as a normal variable, without the array notation.

Variants are special because they can hold many types, including array types, which is why the v = Array() syntax works. When done this way, they should be treated like any other variable and passed that way in and out of subroutines.

like image 101
Bond Avatar answered Nov 07 '22 13:11

Bond