DDV
DDV

Reputation: 2384

Passing Arrays ByRef, Subscript Out of Range

I am trying to pass a number of arrays from my Main subroutine to another subroutine to get loaded. I use ByRef to pass them as pointers so I can continue using them in the Main sub after they have been loaded. I am also passing a dictionary to the load sub.

When I start to loop through the arrays within the Build_Objects sub, I encounter

Subscript Out of Range

My simplified code:

Sub Main()
    
    '//Declarations
    Dim arr_data() As Variant
    Dim dict_headers As Object
    Dim arr_head1() As Variant, arr_head2() As Variant
    Dim arr_platform() As Variant
    
    
    '//Setting
    Set dict_headers = CreateObject("Scripting.Dictionary")
    

    'before main, build objects required
    Call Build_Objects(arr_head1, arr_head2, dict_headers, arr_platform)

    .
    .
    .

End Sub
Sub Build_Objects(ByRef arr_head1P As Variant, ByRef arr_head2P As Variant, ByVal dict_headP As Object, ByRef arr_plat As Variant)
    
    '//Declarations
    Dim i_arr As Long
    ReDim arr_plat(1 To 9, 1 To 4)
    
    
    '//Arrays
    arr_head1P = Array("First Name", "Last Name", "Email", "Phone", "Phone", "Phone", "Phone", "Zip", "Country", "DOB", "Gender")
    arr_head2P = Array("fn", "ln", "email", "Google_ph1", "FB_ph1", "Google_ph2", "FB_ph2", "POSTCODE", "COUNTRY_CODE", "CUSTOMER_DOB", "CUSTOMER_GENDER")
    
    '//Dictionary
    For i_arr = LBound(arr_head1P) To UBound(arr_head1P)
        dict_headP.Add arr_head2P(i_arr), arr_head1P(i_arr)
    Next i_arr
    
End Sub

The Subscript error occurs when i_arr = 1 on the array arr_head2P. But as you can see below, before it enters the For loop the arr_headP2 is loaded correctly and arr_head2P(1) should return ln not an error?

enter image description here

Upvotes: 1

Views: 115

Answers (1)

Ryan Wildry
Ryan Wildry

Reputation: 5677

There is some sort of memory reference issue going on here, replace Dim arr_head1 As Variant(), arr_head2 As Variant() with Dim arr_head1 As Variant, arr_head2 As Variant

Upvotes: 1

Related Questions