+ Reply to Thread
Results 1 to 8 of 8

How to define a constant across all vba code such as your main workbook name.

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    How to define a constant across all vba code such as your main workbook name.

    I have a workbook whose name i would like to define as a constant so that it can be used across all functions and procedures. I cant use "active workbook" as i am opening and closing so many workbooks while this program is running.

    I figured if i somehow define my "main" workbook as a constant then i dont need to keep passing it to all the procedures that need to use it.

    any ideas?
    Last edited by welchs101; 06-09-2011 at 03:51 PM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: How to define a constant across all vba code such as your main workbook name.

    If you want to refer to the workbook in which the code is located, use ThisWorkbook.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: How to define a constant across all vba code such as your main workbook name.

    great idea.......thanks.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: How to define a constant across all vba code such as your main workbook name.

    i would not have to define it as a constant i would just call it "thisworkbook"? is this right.

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to define a constant across all vba code such as your main workbook name.

    why not somethin like:
    Please Login or Register  to view this content.
    Assign this right in the beginning of you code, assuming that your code starts with your Main workbook.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: How to define a constant across all vba code such as your main workbook name.

    Quote Originally Posted by welchs101 View Post
    i would not have to define it as a constant i would just call it "thisworkbook"? is this right.
    Correct. ThisWorkbook always refers to the workbook containing the code.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to define a constant across all vba code such as your main workbook name.

    @Romper, that is an interesting thing to note. I always assumed that ThisWorkbook applied to whatever workbook was active. That is good to know.

    Who are you that is so wise in the ways of science?

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: How to define a constant across all vba code such as your main workbook name.

    Quote Originally Posted by Mordred View Post
    @Romper, that is an interesting thing to note. I always assumed that ThisWorkbook applied to whatever workbook was active.
    Nope - that's ActiveWorkbook. (the clue is in the name. )

    Who are you that is so wise in the ways of science?
    I know a few random things, that's all.

+ 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