Solved

Passing array through subs and functions.

Posted on 2002-03-18
4
165 Views
Last Modified: 2010-05-02
Hi.

I have a dynamic array, and would like to pass it to a sub or function, how can I do so?
sub main()
dim array() as string

redim array(1 to 10)

call subroutine1(array)
end sub


public sub subroutine1(newarray() as string)

end sub

This doesnt work!


Thanks for helping me out.
0
Comment
Question by:samsonite1023
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 6878908
Just like you started it (notice that I have changed your array variable name):

Private Sub Command1_Click()
Dim MyArray() As String
Dim i As Integer

    ReDim MyArray(1 To 10)
    For i = 1 To 10
        MyArray(i) = i
    Next i
   
    Call subroutine1(MyArray)
End Sub


Public Sub subroutine1(newarray() As String)
Dim i As Integer

    For i = LBound(newarray) To UBound(newarray)
        Debug.Print newarray(i)
    Next i
End Sub
0
 
LVL 18

Expert Comment

by:Crash2100
ID: 6878920
it should work fine the way it is, are you actually calling it array() in your program?  Because that's a reserved word, if you are try changing the name.
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 6878939
Just a big mistake on my part!  I had it right on here, but in my actual code I left out the "Call" keyword.

So stupid!

Apparently without the "Call" keyword, it won't work.

Anyone know why?  I thought leaving Call out was perfectly fine.

Thanks

-Sam
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6878946
you may omit the Call but in this case, omit the parentheses too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now