+ Reply to Thread
Results 1 to 10 of 10

Best Way to Declare frequently-used Objects

  1. #1
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Best Way to Declare frequently-used Objects

    Here's my setup:

    A Master Workbook:

    Please Login or Register  to view this content.
    Several tables in the master workbook:

    Please Login or Register  to view this content.
    I'm looking for the best way to define these objects, so I only have to do it once. The procedures are all in the same module (except for the one in a different workbook). So I'm thinking:

    - Can I put Set statements in the Declarations section, in the module above the first procedure?
    -- Does it matter that the two latter tables are destroyed and recreated? Would I have to Set them again afterwards?
    - If not, is there a way to avoid putting the Set statements in every procedure? Can I make a Function to Set them?
    - But if I do, will the Function be called every time I refer to one of these tables or workbook?
    - Can I use Const to define these objects? I'm not sure how to do that.


    Thanks for your help.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,963

    Re: Best Way to Declare frequently-used Objects

    Use Global variables, defined just after this at the top of your codemodule:

    Option Explicit
    Dim wbMasterBook As Workbook
    Dim tblRawData As ListObject
    Dim tblProcessedData As ListObject
    Dim tblMoreData As ListObject

    When you set them, they will retain their value until you release them or Excel closes.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Re: Best Way to Declare frequently-used Objects

    Quote Originally Posted by Bernie Deitrick View Post
    Use Global variables, defined just after this at the top of your codemodule:

    Option Explicit
    Dim wbMasterBook As Workbook
    Dim tblRawData As ListObject
    Dim tblProcessedData As ListObject
    Dim tblMoreData As ListObject

    When you set them, they will retain their value until you release them or Excel closes.
    Okay. But do I have to set them in every procedure that uses them? The table names never change, so duplicating the Set statements seems redundant. What's worse is the workbook name, which changes every few days. I don't want to have to change it in every procedure.

    I was thinking of having another procedure just to run the Set statements, and having it called by the other procedures. Is there a better way?

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,963

    Re: Best Way to Declare frequently-used Objects

    You can use a procedure to set all the values once, and any values that need to change can be changed within the code.

    The workbook to use could be read from a cell on one of the sheets, so there will be no need to change the code:

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Best Way to Declare frequently-used Objects

    ...is there a way to avoid putting the Set statements in every procedure?
    It depends on what you are doing, but there are definitely circumstances within the Excel environment when you need to re-initialise your module variables.

    Can I make a Function to Set them?
    Yes. Well, a call of some sort.

    But if I do, will the Function be called every time I refer to one of these tables or workbook?
    It will, but we take measures to prevent the heavy-lifting work from being re-done when it isn't necessary.

    If I was working with those objects as a related group, then I would use this approach. One nifty advantage of using a Type variable (much older brother of the class module), is that intellisense kicks in so you don't have to type out the member names.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-23-2015
    Location
    Spain
    MS-Off Ver
    2007
    Posts
    32

    Re: Best Way to Declare frequently-used Objects

    Why not try using Static variables?
    Please Login or Register  to view this content.
    It has the adventage that if, for some odd dark reason, Excel loses track of that variable, it checks it back on every run and re-set if needed.
    May the REPUTATION be with me

  7. #7
    Registered User
    Join Date
    09-25-2008
    Location
    Winnetka, CA
    Posts
    83

    Re: Best Way to Declare frequently-used Objects

    Quote Originally Posted by FerJo View Post
    Why not try using Static variables?
    Please Login or Register  to view this content.
    It has the adventage that if, for some odd dark reason, Excel loses track of that variable, it checks it back on every run and re-set if needed.
    Wouldn't the "If wbMasterBook" command have to be in every procedure? Or only whichever procedure I run first that day?

  8. #8
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Best Way to Declare frequently-used Objects

    Static only works if there is only one procedure that needs to use the cached object, which is why I deliberately gave 2 calling procedures in my example above.
    Last edited by cyiangou; 08-24-2015 at 10:47 AM.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Best Way to Declare frequently-used Objects

    Variation on a theme:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-24-2015 at 11:02 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Best Way to Declare frequently-used Objects

    Yeah, I use Types as a form of 'poor mans class module', when my needs don't seem to warrant a whole module.

    In the QuickBasic days, the Type was as close as you got to object-orientation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Frequently Used code
    By jayherring86 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2015, 11:38 AM
  2. Most Frequently Occuring Text
    By Shanie in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 03-25-2015, 12:08 PM
  3. most frequently occuring set
    By apok9f in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-25-2013, 04:29 AM
  4. Excel Freezes frequently
    By priyadva in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 05-24-2013, 09:16 AM
  5. How to hide a selection of objects but show objects automatically when opening wkbk
    By slowjo1414 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-05-2013, 05:24 PM
  6. Most frequently occurring text
    By Deanomcbeano in forum Excel General
    Replies: 4
    Last Post: 08-12-2009, 08:58 AM
  7. most frequently occurring value
    By Pivotrend in forum Excel General
    Replies: 3
    Last Post: 12-23-2005, 08:10 AM

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