Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:16:59+00:00 2026-05-26T04:16:59+00:00

How do I get a public array whose values are set within a subroutine

  • 0

How do I get a public array whose values are set within a subroutine and do not get cleared at the end of the sub in which they were set?

I tried to get:

Public GlobalArray() as Variant

Sub One()
    ReDim GlobalArray(0 to 2)
    GlobalArray=Array("0","1","2")
End Sub

Sub Two()
    Check = GlobalArray(2)
End Sub

such that Check = 2, but I get thrown an error in sub Two complaining about a lack of values in GlobalArray (in fact, even sub One complains that there is no GlobalArray to put things in).

Basically, I have a procedure (One) pulling data from disparate sources, doing some stuff with it, letting the user do some things in Excel, and then running a new subroutine (Two) that uses both the user’s input and some of the arrays from sub One.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:16:59+00:00Added an answer on May 26, 2026 at 4:16 am

    The Public GlobalArray() variable must be declared in a module. It will not work if it is declared at the top of either a Worksheet or the ThisWorkbook module. Try:

    '// Must be declared in a module    
    Public GlobalArray() As Integer
    
    '// These routines can be in worksheet/thisworkbook modules along side events etc Or in a module
    Sub One()
        ReDim GlobalArray(0 To 2)
        GlobalArray(0) = 0
        GlobalArray(1) = 1
        GlobalArray(2) = 2
    End Sub
    
    Sub Two()
        check = GlobalArray(2)
        MsgBox (check)
    End Sub
    

    Instead of a public variable you could pass it to the second function:

    Sub One()
    
        Dim GlobalArray(0 To 2) As Integer
        GlobalArray(0) = 0
        GlobalArray(1) = 1
        GlobalArray(2) = 2
    
        Two GlobalArrayToMe:=GlobalArray
    End Sub
    
    Sub Two(ByRef GlobalArrayToMe() As Integer)
    
        check = GlobalArrayToMe(2)
        MsgBox (check)
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which have automatic properties only like public customerName {get; set;}. They
I have the following class public class Car { public Name {get; set;} }
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
I know I can get the public static members of a class by doing
why doesn't the element get swapped public static void SwapArray(int[,] arr) { for (int
I have the following code: // GET: /PlayRoundHole/Create public ActionResult Create(int id) { DB
I have the following function: //Function to get random number public static int RandomNumber(int
I have multiple controller actions that takes an id public ActionResult Get(int? id) {
Given you have the following class (bad C#, but you get the drift): public
I have a public property set in my form of type ListE<T> where: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.