Passing arrays

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
According to Chip Pearson:


arrays are ALWAYS passed ByRef. If you attempt to pass ByVal, you'll encounter a compilation error.

Here is his code:

Rich (BB code):
Option Explicit
Sub AAATest()
    Dim StaticArray(1 To 3) As Long
    Dim N As Long
    StaticArray(1) = 1
    StaticArray(2) = 2
    StaticArray(3) = 3
    PopulatePassedArray Arr:=StaticArray
    For N = LBound(StaticArray) To UBound(StaticArray)
        Debug.Print StaticArray(N)
    Next N
End Sub
Sub PopulatePassedArray(ByRef Arr() As Long)
    ''''''''''''''''''''''''''''''''''''
    ' PopulatePassedArray
    ' This puts some values in Arr.
    ''''''''''''''''''''''''''''''''''''
    Dim N As Long
    For N = LBound(Arr) To UBound(Arr)
        Arr(N) = N * 10
    Next N
End Sub


In my worksheet, I have the following:

Rich (BB code):
1
2
3

This is my code:

Rich (BB code):
Sub a()
    Dim MyArray() As Variant
    
    MyArray = Cells(1, 1).CurrentRegion.Value
    
    Call b(MyArray)
    
    Debug.Print MyArray(1, 1)
    
End Sub

Sub b(ByRef abc() As Variant)

    abc(1, 1) = 10
    
End Sub

which returns the value of 10 because the Sub b was passed ByRef and has changed the array's value.

If I wrote this, it would NOT compile, as per Chip Pearson's article:

Rich (BB code):
Sub a()

    Dim MyArray() As Variant
    
    MyArray = Cells(1, 1).CurrentRegion.Value
    
    Call b(MyArray)
    
    Debug.Print MyArray(1, 1)
    
End Sub

Sub b(ByVal abc() As Variant)

    abc(1, 1) = 10
    
End Sub

However, if I omitted the brackets, like this:

Rich (BB code):
Sub a()

    Dim MyArray() As Variant
    
    MyArray = Cells(1, 1).CurrentRegion.Value
    
    Call b(MyArray)
    
    Debug.Print MyArray(1, 1)
    
End Sub

Sub b(ByVal abc As Variant)

    abc(1, 1) = 10
    
End Sub

it returns the value of 1, as "sort of" expected because Sub b was passed ByVal.

So in my last example, have I passed an array ByVal or have I passed something else ByVal?

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Thanks

Examples of data types include Integer, Long, Single, Double, String, Boolean, Object, etc.

My understanding is a Variant is no specific data type.

An Array is a data structure, as is a dictionary or collection.

So what does it mean to "pass a variant"?
 
Upvote 0
As far as I know a variant can hold any type of data, and I would assume that would include an array.
 
Upvote 0
Thanks that's what I'm uneasy about.

Variant is data type (though nothing specific) and an array is a data structure, so it seems it's possible to not only pass a data type as an argument but a data structure too.

Therefore I assume it's possible to pass a dictionary or a collection.
 
Upvote 0
Don't see a problem passing a dictionary or collection.
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,365
Members
449,155
Latest member
ravioli44

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