+ Reply to Thread
Results 1 to 9 of 9

Access array created in another subroutine

  1. #1
    Registered User
    Join Date
    09-28-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Access array created in another subroutine

    What is the most efficient way to access an array that is created in another subroutine.

    An option for me is to keep the array and the code that accesses it in the same subroutine, but it causes a duplication of the same code because I need to do something several times.

    Thank you in advance!

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Access array created in another subroutine

    Declare the array as a public(or even private) variable at the head of the module, it is then available to all procedures in the project (or just that module if private).

  3. #3
    Registered User
    Join Date
    09-28-2010
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Access array created in another subroutine

    Thanks Bob.
    Out of curiosity, is this more efficient than passing it by reference?
    Last edited by GunNam; 12-23-2010 at 04:24 AM. Reason: changed they to than so the sentence would make sense.

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Access array created in another subroutine

    Efficient? I doubt there is anything in it, but it does of course use more memory because the variables are in scope for longer. But memory is rarely a consideration. I don't use such variables much personally, but that is because of my coding style/paradigm, not for efficiency or memory considerations.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Access array created in another subroutine

    An option for me is to keep the array and the code that accesses it in the same subroutine, but it causes a duplication of the same code because I need to do something several times.
    Can you explain what you mean by this ? I can't imagine any problem doing this.



  6. #6
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Access array created in another subroutine

    Quote Originally Posted by snb View Post
    Can you explain what you mean by this ? I can't imagine any problem doing this.
    Code duplication is a bad idea, if you need to change a routine, you need to change it in several places.

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Access array created in another subroutine

    I do not understand what you mean by this. Please give an example.

  8. #8
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Access array created in another subroutine

    You are asking a question about someone else's point that you don't understand, so I think you would not benefit from my elaborating on my previous reply. I see this as a non-effective use of my time, so I decline.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Access array created in another subroutine

    That's what puzzled me in your reaction to my question to the OP in the first place.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1