Off97: How to Create and Use a Global Array in VBA Procedures

Last reviewed: November 13, 1997
Article ID: Q170721
The information in this article applies to:
  • Microsoft Visual Basic for Applications included with:

        - Microsoft Word 97 for Windows
        - Microsoft PowerPoint 97 for Windows
        - Microsoft Excel 97 for Windows
        - Microsoft Access versions 7.0, 97
    

SUMMARY

In Microsoft Visual Basic for Applications, you can create a dimensioned array, such as A(6,6), that is available to all procedures in all modules.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Follow these steps to create sample Visual Basic procedures that fill and display a public array.

  1. Type the following statement in the Declarations section of a module:

          Public MyArray(1) as String
    

        Note: You cannot declare Publicly within an Object module. An example of
        an Object module is 'ThisDocument'. 
    

        To insert a new module, while in the Visual Basic Editor, on the Insert
        menu click Module.
    

  2. Type the following Sub procedures in a module:

          Sub FillArray()
    
             ' Fill the array MyArray with values.
             MyArray(0) = "Hi"
             MyArray(1) = "Bye"
    
             ' Call the DisplayArray Sub procedure to display MyArray.
             DisplayArray
    
          End Sub
    
          Sub DisplayArray()
    
             ' Display the values contained in the array MyArray.
             For i = 0 to Ubound(MyArray, 1)
                MsgBox MyArray(i)
             Next
    
          End Sub
    
    

  3. Run the FillArray Sub procedure.

REFERENCES

For more information about public arrays, click the Office Assistant, type "Public," click Search, and then click to view "Public Statement."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office Program or
               Component


Additional query words: 8.0 vba vbe vb
Keywords : kbcode kbmacro kbprg
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.