Need help creating, manipulating and reading global arrays ...

E

eclectic.01

I have been trawling for days for what must be VERY simple ... I just
cant find it :(

I would like to create a global array, populate it from one function,
wait for user interaction, then read from it in another. Sound simple?
Well ...

I have tried the following (the most simple example I could think of):

Public myArray(0 To 10) As String

Sub Test()
myArray(0) = "Test"
MsgBox (myArray(0))
End Sub

When trying to run 'Test', I receive the error "Sub or Function not
defined" on the "myArray(0) = "Test"" line ...

Regardless of setting 'myArray' as public, 'Test' is not recognizing
the global array ... it thinks it is a function ...

The below works, but of course in this example 'myArray'is not global,
which is what I need:

Sub Test()
Dim myArray(0 To 10) As String
myArray(0) = "Test"
MsgBox (myArray(0))
End Sub

What must I do to actually create and then use a global array? I need
help quick! running out of hair!

Cheers!
 
T

Tom Ogilvy

Public myArray(0 To 10) As String

Sub Test()
myArray(0) = "Test"
MsgBox (myArray(0))
End Sub

worked fine for me in a general module. (at least the Public statement must
be in a general module [Insert=>Modlue] in the same workbook.
 
J

Jim Cone

It works for me. I get a message box with "Test".
The public declaration should be in a standard module, not the
ThisWorkbook module or a module attached to a sheet.
Also, the ( ) around myArray(0) is not required.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


<[email protected]>
wrote in message
I have been trawling for days for what must be VERY simple ... I just
cant find it :(
I would like to create a global array, populate it from one function,
wait for user interaction, then read from it in another. Sound simple?
Well ...
I have tried the following (the most simple example I could think of):

Public myArray(0 To 10) As String

Sub Test()
myArray(0) = "Test"
MsgBox (myArray(0))
End Sub

When trying to run 'Test', I receive the error "Sub or Function not
defined" on the "myArray(0) = "Test"" line ...
Regardless of setting 'myArray' as public, 'Test' is not recognizing
the global array ... it thinks it is a function ...
The below works, but of course in this example 'myArray'is not global,
which is what I need:

Sub Test()
Dim myArray(0 To 10) As String
myArray(0) = "Test"
MsgBox (myArray(0))
End Sub

What must I do to actually create and then use a global array? I need
help quick! running out of hair!
Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top