Austin Jones
Austin Jones

Reputation: 709

Passing an array to subroutine VBA

I'm working on a macro for excel and have a sub that passes an array to another sub, but I keep getting

Run time error '9'

Subscript out of range

below is my code and I left a comment pointing to where this error is occurring. I'm new to VBA so it's possible I'm trying to pass an array incorrectly not sure though.

'Main Driver
Sub Main()
    WorkbookSize = size() 'Run function to get workbook size
    newbook = False
    Call create            'Run sub to create new workbook
    Call pull(WorkbookSize)              'Run sub to pull data
End Sub

'Get size of Worksheet
Function size() As Integer
    size = Cells(Rows.Count, "A").End(xlUp).Row
End Function

'Create workbook
Sub create()
    Dim wb As Workbook
    Set wb = Workbooks.Add
    TempPath = Environ("temp")
    With wb
        .SaveAs Filename:=TempPath & "EDX.xlsm" _
        , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

        .ChangeFileAccess Mode:=xlReadOnly, WritePassword:="admin"
    End With
End Sub

'pull data
Sub pull(size)
    Dim code() As Variant
    For i = 1 To size
    'Check code column fo IN and Doctype column for 810
        If Cells(i, 18).Value = "IN" Then
            code(i) = Cells(i, 18).Value 'store in array
        End If
    Next i
     Call push(code)
End Sub

'push data to new workbook
Sub push(ByRef code() As Variant)
    activeBook = "TempEDX.xlsm"
    Workbooks(activeBook).Activate 'set new workbook as active book
    For i = 1 To UBound(code)   ' <---here is where the error is referencing
        Cells(i, 1).Value = code(i)
    Next i
End Sub

Any help is appreciated.

Upvotes: 0

Views: 11638

Answers (2)

GisMofx
GisMofx

Reputation: 1024

Your problem is that you don't correctly initialize the code array.

Do so using Redim See the modification below:

    'pull data
    Sub pull(size)
        Dim code() As Variant
        Redim code(size-1)  '<----add this here minus 1 because 0 index array
        For i = 1 To size
        'Check code column fo IN and Doctype column for 810
            If Cells(i, 18).Value = "IN" Then
                code(i-1) = Cells(i, 18).Value 'store in array subtract 1 for 0 index array
            End If
        Next i
         Call push(code)
    End Sub

Also, you'll need to update your Push method's code to accommodate the 0-indexed array

'push data to new workbook
Sub push(ByRef code() As Variant)
    activeBook = "TempEDX.xlsm"
    Workbooks(activeBook).Activate 'set new workbook as active book
    For i = 0 To UBound(code)   ' <0 to ubound
        Cells(i+1, 1).Value = code(i) 'add 1 to i for the cells reference
    Next i
End Sub

Upvotes: 3

QHarr
QHarr

Reputation: 84465

I will add these other points

You are also working with Rows but have Integer as a return for a function risking overflow e.g.

Function size() As Integer

Change to a Long.

You have lots of implicit activesheet references. Get rid of those and give a parent sheet. For example, you could set the sheet in a variable called ws and pass, if required, as a parameter.

E.g.

Public Function size(ByVal ws As Worksheet) As Long
    With ws
        size = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
End Function

As mentioned, put Option Explicit at the top of your code and declare all your variables.

Upvotes: 2

Related Questions