Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 333
Default Pass an array

Hi,

How do I pass an array to a sub routine?

Dim HeaderArray(1 to 6) as long

HeaderArray(1) = 43
HeaderArray(2) = 90
HeaderArray(3) = 138
HeaderArray(4) = 186
HeaderArray(5) = 234
HeaderArray(6) = 282

I tried passing it as HeaderArray but got strange results.

Call NextRoutine(HeaderArray)

--
Thanks for your help.
Karen53
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Pass an array

Karen,

How have you declared your NextRoutine procedure? You need to declare the
input paramater as an array of the exact same data type as the array being
passed, or declare it as a Variant (not as an array of variants). For
example,

Sub CalledProc(Arr() As Long)
Dim N As Long
For N = LBound(Arr) To UBound(Arr)
Debug.Print Arr(N)
Next N
End Sub

You can call this with code like:

Sub AAA()
Dim Arr(1 To 3) As Long
Dim L As Long

Arr(1) = 11
Arr(2) = 22
Arr(3) = 33

CalledProc Arr
End Sub

Since CalledProc expects an array of Longs, you'll get an error if you try
to pass an array of any other data type to it. The alternative is to declare
the input parameter as a Variant. For example,

Sub CalledProc(Arr As Variant)
Dim N As Long
If IsArray(Arr) = True Then
For N = LBound(Arr) To UBound(Arr)
Debug.Print Arr(N)
Next N
Else
Debug.Print "not an array"
End If
End Sub

This can accept an array of any data type and data that is not an array. It
tests the input parameter with IsArray to see if it was passed an array,.

See http://www.cpearson.com/Excel/Passin...ningArrays.htm for more
information and example code about passing arrays to and from procedures.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)



"Karen53" wrote in message
...
Hi,

How do I pass an array to a sub routine?

Dim HeaderArray(1 to 6) as long

HeaderArray(1) = 43
HeaderArray(2) = 90
HeaderArray(3) = 138
HeaderArray(4) = 186
HeaderArray(5) = 234
HeaderArray(6) = 282

I tried passing it as HeaderArray but got strange results.

Call NextRoutine(HeaderArray)

--
Thanks for your help.
Karen53


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Pass an array

Can you show us the NextRoutine's declaration statement please?

Rick

How do I pass an array to a sub routine?

Dim HeaderArray(1 to 6) as long

HeaderArray(1) = 43
HeaderArray(2) = 90
HeaderArray(3) = 138
HeaderArray(4) = 186
HeaderArray(5) = 234
HeaderArray(6) = 282

I tried passing it as HeaderArray but got strange results.

Call NextRoutine(HeaderArray)


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 333
Default Pass an array


Thank You, Chip!
--
Thanks for your help.
Karen53


"Chip Pearson" wrote:

Karen,

How have you declared your NextRoutine procedure? You need to declare the
input paramater as an array of the exact same data type as the array being
passed, or declare it as a Variant (not as an array of variants). For
example,

Sub CalledProc(Arr() As Long)
Dim N As Long
For N = LBound(Arr) To UBound(Arr)
Debug.Print Arr(N)
Next N
End Sub

You can call this with code like:

Sub AAA()
Dim Arr(1 To 3) As Long
Dim L As Long

Arr(1) = 11
Arr(2) = 22
Arr(3) = 33

CalledProc Arr
End Sub

Since CalledProc expects an array of Longs, you'll get an error if you try
to pass an array of any other data type to it. The alternative is to declare
the input parameter as a Variant. For example,

Sub CalledProc(Arr As Variant)
Dim N As Long
If IsArray(Arr) = True Then
For N = LBound(Arr) To UBound(Arr)
Debug.Print Arr(N)
Next N
Else
Debug.Print "not an array"
End If
End Sub

This can accept an array of any data type and data that is not an array. It
tests the input parameter with IsArray to see if it was passed an array,.

See http://www.cpearson.com/Excel/Passin...ningArrays.htm for more
information and example code about passing arrays to and from procedures.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)



"Karen53" wrote in message
...
Hi,

How do I pass an array to a sub routine?

Dim HeaderArray(1 to 6) as long

HeaderArray(1) = 43
HeaderArray(2) = 90
HeaderArray(3) = 138
HeaderArray(4) = 186
HeaderArray(5) = 234
HeaderArray(6) = 282

I tried passing it as HeaderArray but got strange results.

Call NextRoutine(HeaderArray)

--
Thanks for your help.
Karen53


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 333
Default Pass an array


--
Thanks for your help.
Karen53


"Karen53" wrote:

Hi,

How do I pass an array to a sub routine?

Dim HeaderArray(1 to 6) as long

HeaderArray(1) = 43
HeaderArray(2) = 90
HeaderArray(3) = 138
HeaderArray(4) = 186
HeaderArray(5) = 234
HeaderArray(6) = 282

I tried passing it as HeaderArray but got strange results.

Call NextRoutine(HeaderArray)

--
Thanks for your help.
Karen53



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Pass an array

Karen53 wrote:

What "strange results" did you get?

Alan Beban
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
combining cells and array from different sheets into an array to pass to IRR() [email protected] Excel Discussion (Misc queries) 3 September 11th 06 07:17 AM
Pass an array to Rank Biff Excel Worksheet Functions 12 June 29th 05 04:15 PM
How do I pass an array to a listbox? Titus A Ducksass - AKA broken-record Excel Programming 4 March 23rd 05 07:20 PM
How can I pass an array as TextToDisplay to a hyperlink? Jay Fincannon Excel Programming 4 January 28th 05 01:45 AM
Pass array from Project to Excel Sarah[_5_] Excel Programming 2 April 12th 04 07:30 PM


All times are GMT +1. The time now is 07:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"