musbach
musbach

Reputation: 638

VBA Word How do I pass part of an array to a subroutine?

I want to pass a part of an array to a subroutine!

Sub main()
    Dim i(10) As Byte
    Dim j As Integer

    For j = LBound(i) To UBound(i)
        i(j) = j
    Next
    
    Call subr(i())

End Sub
Sub subr(i() As Byte)
    Dim j As Integer

    For j = LBound(i) To UBound(i)
        Debug.Print  i(j)
    Next
    
End Sub

This example passes the entire array to subr and prints it.

I want something like this (which doesn't work):

Call subr(i(L_index to U_index))

L_index and U_index determine the range to pass.

Upvotes: 1

Views: 96

Answers (1)

Alex K.
Alex K.

Reputation: 175816

The only way to do that would be to copy the span of the array your interested in, instead pass the array & range:

Call subr(i(), 3, 5)

Sub subr(arr() As Byte, startIndex As Long, endIndex As Long)
   
   For i = startIndex To endIndex
        Debug.Print arr(i)
   Next
    
End Sub

Upvotes: 2

Related Questions