Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default VBA: How to pass arrays in Function Calls?

You are assigning the first element of an array of integers with the string
Won. This array can only hold integers. Try this...

Function Testx(Arg)
' Function should return the word "Won".
Dim Cols(3) As Integer
Call Testy(Cols())
Testx = Cols(1)
End Function

Sub Testy(Cols() As Integer)
Cols(1) = 123
MsgBox "Done" <-- Calling never gets to this line.
End Sub

--
HTH...

Jim Thomlinson


"Mac Lingo" wrote:

This code dies at the "Msgbox" Line.

Can you give me an idea why. And what do I do to make it work as it should?

Function Testx(Arg)
' Function should return the word "Won".
Dim Cols(3) As Integer
Call Testy(Cols())
Testx = Cols(1)
End Function

Sub Testy(Cols() As Integer)
Cols(1) = "Won"
MsgBox "Done" <-- Calling never gets to this line.
End Sub

Thanks,
Mac Lingo
Berkeley, CA



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default VBA: How to pass arrays in Function Calls?

More generally, make use of Paramarray to pass back and forth whatever
you like !

Example:

Put this in the declare space:

Public MyCols() 'Default to Variant data type;
also Cols is not a
'great idea for a
variable name as it is reserved to
'VB in many contexts

Then try the following procedures:

Function Testx(Arg)
' Function should return the word "Won".
Call Testy(MyCols())
Testx = MyCols(1)
End Function


Sub Testy(ParamArray MyCols() As Variant)
MyCols(1) = "Won"
MsgBox "Done" <-- Calling never gets to this line.
End Sub

Nick

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default How to pass arrays in Function Calls?

Mac,

Firstly, if you want to assign "Won" to an element in your array you'll have
to declare it as type string, not integer. That may be your problem there.
Also note that an array declared arr(3) has 4 elements, not 3. (0 is the
first unless option base is specified.)

Your Arg doesn't appear to be doing anything, maybe you meant Function
Testx() as <type ?

Otherwise I can see no reason your code wouldn't run.

-Mike

"Mac Lingo" wrote in message
m...
This code dies at the "Msgbox" Line.

Can you give me an idea why. And what do I do to make it work as it
should?

Function Testx(Arg)
' Function should return the word "Won".
Dim Cols(3) As Integer
Call Testy(Cols())
Testx = Cols(1)
End Function

Sub Testy(Cols() As Integer)
Cols(1) = "Won"
MsgBox "Done" <-- Calling never gets to this line.
End Sub

Thanks,
Mac Lingo
Berkeley, CA



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default VBA: How to pass arrays in Function Calls?

This code dies at the "Msgbox" Line.

Can you give me an idea why. And what do I do to make it work as it should?

Function Testx(Arg)
' Function should return the word "Won".
Dim Cols(3) As Integer
Call Testy(Cols())
Testx = Cols(1)
End Function

Sub Testy(Cols() As Integer)
Cols(1) = "Won"
MsgBox "Done" <-- Calling never gets to this line.
End Sub

Thanks,
Mac Lingo
Berkeley, CA


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default VBA: How to pass arrays in Function Calls?

There really is no role for a parameter array in this unless the user is
going to pass elements of his array individually.

To use a parameter array for this, you would have to do it this way:

Sub ABC()
Debug.Print Testx("abc")
End Sub

Function Testx(Arg)
' Function should return the word "Won".
Dim MyCols(1 To 3) As Variant
Call Testy(MyCols())
Testx = MyCols(1)
End Function


Sub Testy(ParamArray MyCols() As Variant)
MyCols(LBound(MyCols))(1) = "Won"
MsgBox "Done"
End Sub

So you would have your original array in the first element of the parameter
array. More complex than necessary and outside the intent of the OP.

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
More generally, make use of Paramarray to pass back and forth whatever
you like !

Example:

Put this in the declare space:

Public MyCols() 'Default to Variant data type;
also Cols is not a
'great idea for a
variable name as it is reserved to
'VB in many contexts

Then try the following procedures:

Function Testx(Arg)
' Function should return the word "Won".
Call Testy(MyCols())
Testx = MyCols(1)
End Function


Sub Testy(ParamArray MyCols() As Variant)
MyCols(1) = "Won"
MsgBox "Done" <-- Calling never gets to this line.
End Sub

Nick



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
using strings in vlookup function calls AtTheEndofMyRope Excel Worksheet Functions 5 August 30th 09 07:06 PM
excel vba problem - function calls from cells internerdj Excel Programming 6 July 7th 04 10:18 PM
worksheet function calls between worksheets Joe Excel Programming 0 April 29th 04 03:21 PM
identifying which cell calls my VBA function. y Excel Programming 3 April 26th 04 04:32 AM
function calls Claude Excel Programming 2 December 5th 03 01:55 PM


All times are GMT +1. The time now is 08:44 PM.

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

About Us

"It's about Microsoft Excel"