Passing an array to subroutine VBA

Austin Jones

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.

GisMofx

You're 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Perl - passing an array to subroutine

From Dev

Passing array, scalar and hash to subroutine in Perl

From Dev

Passing array, scalar and hash to subroutine in Perl

From Dev

Perl, passing array into subroutine, dealing with undeclared variables

From Dev

Perl Array dereference not working properly while passing it as an argument to a subroutine

From Dev

Perl Array dereference not working properly while passing it as an argument to a subroutine

From Dev

passing hashes to subroutine

From Dev

passing hashes to subroutine

From Dev

VBA EXCEL: How to call a subroutine in another subroutine?

From Dev

batch script subroutine: Passing arguments

From Dev

Passing an FFTW plan to a Fortran subroutine

From Dev

FORTRAN: passing a dummy argument to a subroutine

From Dev

batch script subroutine: Passing arguments

From Dev

Passing an FFTW plan to a Fortran subroutine

From Dev

FORTRAN: passing a dummy argument to a subroutine

From Dev

using real(A[,kind=]) in passing a single precision array as a double precision to a subroutine in Fortran 90/2003

From Dev

Memory leakage issue in FORTRAN when allocating an array inside a subroutine and passing it back

From Dev

passing array as parameter of RunPython script in VBA (xlwings)

From Dev

Subroutine that processes the array it returns?

From Dev

Subroutine with array element as argument

From Dev

FORTRAN - allocatable array in subroutine

From Dev

optimizing an array sorting subroutine

From Dev

VBA include argument to subroutine in OnAction

From Dev

Calling excel VBA subroutine with arguments

From Java

Passing variable from subroutine in tk perl interface

From Dev

Passing a set of explicit strings to a Fortran subroutine

From Dev

fortran, passing allocatable arrays to a subroutine with right bounds

From Dev

Fortran: passing arbitrary "structures" to a module subroutine

From Dev

Microsoft batch file not passing arguments to subroutine

Related Related

  1. 1

    Perl - passing an array to subroutine

  2. 2

    Passing array, scalar and hash to subroutine in Perl

  3. 3

    Passing array, scalar and hash to subroutine in Perl

  4. 4

    Perl, passing array into subroutine, dealing with undeclared variables

  5. 5

    Perl Array dereference not working properly while passing it as an argument to a subroutine

  6. 6

    Perl Array dereference not working properly while passing it as an argument to a subroutine

  7. 7

    passing hashes to subroutine

  8. 8

    passing hashes to subroutine

  9. 9

    VBA EXCEL: How to call a subroutine in another subroutine?

  10. 10

    batch script subroutine: Passing arguments

  11. 11

    Passing an FFTW plan to a Fortran subroutine

  12. 12

    FORTRAN: passing a dummy argument to a subroutine

  13. 13

    batch script subroutine: Passing arguments

  14. 14

    Passing an FFTW plan to a Fortran subroutine

  15. 15

    FORTRAN: passing a dummy argument to a subroutine

  16. 16

    using real(A[,kind=]) in passing a single precision array as a double precision to a subroutine in Fortran 90/2003

  17. 17

    Memory leakage issue in FORTRAN when allocating an array inside a subroutine and passing it back

  18. 18

    passing array as parameter of RunPython script in VBA (xlwings)

  19. 19

    Subroutine that processes the array it returns?

  20. 20

    Subroutine with array element as argument

  21. 21

    FORTRAN - allocatable array in subroutine

  22. 22

    optimizing an array sorting subroutine

  23. 23

    VBA include argument to subroutine in OnAction

  24. 24

    Calling excel VBA subroutine with arguments

  25. 25

    Passing variable from subroutine in tk perl interface

  26. 26

    Passing a set of explicit strings to a Fortran subroutine

  27. 27

    fortran, passing allocatable arrays to a subroutine with right bounds

  28. 28

    Fortran: passing arbitrary "structures" to a module subroutine

  29. 29

    Microsoft batch file not passing arguments to subroutine

HotTag

Archive