LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default how pass array to a function?

For n = 1 to 9
If Array(n) = x then MyFunction = True
Next


Hi. Would this idea help?

Sub TestIt()
Dim v
v = Array(1, 2, 3, "Cat", 4, 5)

Debug.Print MemberQ(6, v) 'Is 6 in v?
Debug.Print MemberQ(4, v)
Debug.Print MemberQ("Dog", v)
Debug.Print MemberQ("Cat", v)
End Sub

Returns:
False
True
False
True

Function MemberQ(x, M) As Boolean
'// Is x a member of Array M ?
On Error Resume Next
MemberQ = WorksheetFunction.Match(x, M, 0) 0
End Function

= = = = = = = = = =
HTH :)
Dana DeLouis


On 3/19/2010 1:44 AM, John wrote:
I sort of assumed I had dimmed the array and filled it with values
before the function.

So: Dim Array(9) as long
Dim n as long
For N = 1 to 9
Array(n)=n
Next

The following creates and error. Also if i just use the Array(9) in the
function it creates and error.

Public Function MyFunction(x as long,Array() as long) as Boolean
Dim n as Long
MyFunction=False
For n = 1 to 9
If Array(n) = x then MyFunction = True
Next
End Function

I'm not sure if what you suggested gets around that. Or are you saying I
just can't include that array inside the function period?

John

Dennis Tucker wrote:
Do not pass an array!

The key here is the "Scope of Variables".

At the top of the module, not inside any Sub or Function, put the
declaration of the array.

Dim MyArray(3,35) as string (just an example)

Now MyArray() has a wider scope. The scope in this case is the current
module(or userform).

For even wider scope use this

Public MyArray(3,35) as string (just an example)

The scope in this case is the current project(all userforms and
modules inside the current project.

Dennis

"John" wrote in message
...
If you have an Array, say Array(9), how do you pass that information
for a function to use?

For example:
Public Function MyFunction(x as long) as Boolean
Dim n as Long
MyFunction=False
For n = 1 to 9
If Array(n) = x then MyFunction = True
Next
End Function

I know you can't have Public Function MyFunction(x as Long, Array()
as long) as Boolean"

But there has to be some way for public functions to access arrays
outside the function doesn't there?

Thanks

John



--
= = = = = = =
HTH :)
Dana DeLouis
 
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
How to pass a dynamic array FROM a function TO a sub Al[_19_] Excel Programming 0 August 17th 09 08:44 PM
Pass an array Karen53 Excel Programming 5 November 28th 07 02:17 AM
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


All times are GMT +1. The time now is 04:12 AM.

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"