Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing arrays to a subroutine

Hello,

I have a simple question. I would like to know how
to pass an array argument to a subroutine. I would like
to do something like the example below illustrates. (The
syntax for the below example is wrong and does not work
but should show the simple thing I am trying to do.)

I can program a workaround using ParamArray, but I would
prefer not to pass my args in this way. Instead of
listing all the args of my array in the ParamArray, I
would prefer to pack them into an array and pass them to
my subroutine using just the name of the array.

Thanks in advance for any help with this matter.

Braden Craig


Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub (argArray)

End Sub


Public Sub testerSub(argArray() As Variant)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Passing arrays to a subroutine

Hi Braden,

This works for me:

Public Sub arrayPass()
Dim argArray(3) As Integer
argArray(0) = 12
argArray(1) = 13
testerSub argArray
End Sub

Public Sub testerSub(argArray() As Integer)
Dim test1 As Integer
Dim test2 As Integer
test1 = argArray(0)
test2 = argArray(1)
Debug.Print test1, test2
End Sub

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"Braden Craig" wrote in message
...
Hello,

I have a simple question. I would like to know how
to pass an array argument to a subroutine. I would like
to do something like the example below illustrates. (The
syntax for the below example is wrong and does not work
but should show the simple thing I am trying to do.)

I can program a workaround using ParamArray, but I would
prefer not to pass my args in this way. Instead of
listing all the args of my array in the ParamArray, I
would prefer to pack them into an array and pass them to
my subroutine using just the name of the array.

Thanks in advance for any help with this matter.

Braden Craig


Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub (argArray)

End Sub


Public Sub testerSub(argArray() As Variant)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Passing arrays to a subroutine

Your code is almost right, the only change I made is I
removed the parenthesis around the argArray argument
array when calling the testerSub procedure.

Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub argArray

End Sub


Public Sub testerSub(argArray As Variant)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)

End Sub
-----Original Message-----
Hello,

I have a simple question. I would like to know how
to pass an array argument to a subroutine. I would like
to do something like the example below illustrates.

(The
syntax for the below example is wrong and does not work
but should show the simple thing I am trying to do.)

I can program a workaround using ParamArray, but I would
prefer not to pass my args in this way. Instead of
listing all the args of my array in the ParamArray, I
would prefer to pack them into an array and pass them to
my subroutine using just the name of the array.

Thanks in advance for any help with this matter.

Braden Craig


Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub (argArray)

End Sub


Public Sub testerSub(argArray() As Variant)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)

End Sub
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Passing arrays to a subroutine

This worked fine for me:

Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub argArray

End Sub


Public Sub testerSub(argArray() As Integer)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)
Debug.Print test1
Debug.Print test2
End Sub


--
Regards,
Tom Ogilvy

Braden Craig wrote in message
...
Hello,

I have a simple question. I would like to know how
to pass an array argument to a subroutine. I would like
to do something like the example below illustrates. (The
syntax for the below example is wrong and does not work
but should show the simple thing I am trying to do.)

I can program a workaround using ParamArray, but I would
prefer not to pass my args in this way. Instead of
listing all the args of my array in the ParamArray, I
would prefer to pack them into an array and pass them to
my subroutine using just the name of the array.

Thanks in advance for any help with this matter.

Braden Craig


Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
testerSub (argArray)

End Sub


Public Sub testerSub(argArray() As Variant)
Dim test1 As Integer, test2 As Integer

test1 = argArray(0)
test2 = argArray(1)

End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Passing arrays to a subroutine

As does this:

Public Sub arrayPass()
Dim argArray(3) As Integer

argArray(0) = 12
argArray(1) = 13
MsgBox testerSub(argArray)

End Sub


Public Function testerSub(inputArray As Variant)
Dim test1 As Integer, test2 As Integer

test1 = inputArray(0)
test2 = inputArray(1)
testerSub = test1 & " " & test2

End Function

Alan Beban

Rob Bovey wrote:
Hi Braden,

This works for me:

Public Sub arrayPass()
Dim argArray(3) As Integer
argArray(0) = 12
argArray(1) = 13
testerSub argArray
End Sub

Public Sub testerSub(argArray() As Integer)
Dim test1 As Integer
Dim test2 As Integer
test1 = argArray(0)
test2 = argArray(1)
Debug.Print test1, test2
End Sub


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
Call a subroutine using variable subroutine name dhstein Excel Discussion (Misc queries) 3 July 26th 09 08:28 PM
Stopping a userform subroutine teepee[_3_] Excel Discussion (Misc queries) 5 January 13th 09 01:10 AM
SUBROUTINE HELP biker man Excel Discussion (Misc queries) 1 July 28th 07 04:06 PM
passing arrays to user defined functions ramki Excel Worksheet Functions 2 February 15th 06 08:34 AM
How do I exit a macro subroutine? John Excel Worksheet Functions 1 January 15th 06 02:08 AM


All times are GMT +1. The time now is 06:15 PM.

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"