Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to setup a global array????????

Status
Not open for further replies.

Mightyginger

Programmer
Feb 27, 2003
131
US
Another quick question please guys. I need to make an array global so that other sub and functions can access it - how do I do this?

I would be using the Sub uto_Open to get it to intially read some data from a file into an array and then instead of having to query the file it just queries the array.


Thanks again folks,



Neil.
 
Why do I keep on thinking your name is Minty Ginger? :-S

If you want a global scope to your array it's just the same as for "normal" variables.

eg declare array in Module1 which also contains sub to set up array:

Code:
Public str_arr() As String
Dim i As Integer
Sub set_up_arr()
ReDim str_arr(4)
For i = 0 To 4
    str_arr(i) = "Mr Man " & i + 1
    Next
End Sub

Note the use of "Public" rather than "Dim" - this ensures code in otehr modules can "see" array.

I used array in Sheet1 module by tying it to worksheet change event:

Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
For i = 0 To 4
    MsgBox str_arr(i)
    Next
End Sub

try it for yourself - you should be able to get it working.

Moral is - just use
Code:
Public
keyword.

 
No, it's appreciated. I just put Global for the variable at the very top ofeverything - so not in any functions or subs and then fill it in a sub and use it in another sub. Seems to work.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top