+ Reply to Thread
Results 1 to 5 of 5

Setting module-level object variables

  1. #1
    Registered User
    Join Date
    12-04-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Setting module-level object variables

    Maybe I'm using the wrong term here, but is it possible to set an object variable at the module or global level?

    Specifically I simply wish to name a given cell (well, over 100 of them) and then be able to use that name in the module's Sub routines, for example,

    Set name1 = Range("a1")

    and then

    If name1 = true Then
    etc, etc

    in a Sub procedure.

    This works without a problem when I place it inside a Sub, but when I try it up at the beginning of a module, it tells me that it's an "invalid outside procedure".
    I figure it'll be helpful if I don't have to declare these over and over again in each Sub procedure.

    Thanks in advance
    Chris

  2. #2
    Registered User
    Join Date
    04-12-2013
    Location
    Sacramento, CA
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Setting module-level object variables

    If you declare the variable above/outside of the sub it should retain a value through any sub called in that module. So if you declare the variable, then immediately initialize/define in your first sub run it should work like you want. An example:

    Please Login or Register  to view this content.
    Other than that... I don't think so.


    EDIT: Sorry, I get to typing code and that tab button becomes public enemy # 1...
    Last edited by crosservice; 04-26-2013 at 06:15 PM.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,509

    Re: Setting module-level object variables

    You can't Set the value outside a procedure.

    You can define (Dim) the variable before all the subroutines and functions but you need to give it a value in one of the procedures. Thus:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    12-04-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Setting module-level object variables

    Thank you TMS and crosservice. Works perfectly.

    I just spent several fruitless hours trying to find the answer myself and you guys pop it up in less than 15 minutes. As a novice, it's very much appreciated.
    Thank you kindly.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,509

    Re: Setting module-level object variables

    You're welcome. Thanks for the rep.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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