+ Reply to Thread
Results 1 to 7 of 7

ThisWorkbook vs. Module1

  1. #1
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    ThisWorkbook vs. Module1

    This may be a zen question rather than a "rule" one.

    If I want code to be on demand for a whole workbook I use Module1 and I've done so forever and ever. I probably got hooked because macro recorder lands there, in Module1 or Module2 (etc.). (* see footnote)

    It just occurred to me, why not just use the ThisWorkbook area? (Double click ThisWorkbook in Ctrl-R Project Explorer)

    As I've been doing it, my code goes in modules, and ThisWorkbook code is just for events (e.g. Sub Workbook_BeforeSave). Should I consider using ThisWorkbook for my code; or should I just leave that for events, and use modules for my code?


    * footnote: Interestingly, the macro recorder (by default) "says" it's putting the code in "This Workbook" - but it really refers to a Module in activeworkbook, not activeworkbook's code section ThisWorkbook.
    Chip, if you can read this, you're STILL making the world a better place with your immeasurably valuable gifts. Every week, you continue to help me (and untold numbers of others) tremendously. Practically no alternative outlets can match your effectiveness, even over the vast number of topics you demystify and empower usage of. You were, and still are, amazing.

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: ThisWorkbook vs. Module1

    I typically have explicitly called out modules with specific names, NOT module1. If there is a workbook event I put it in the workbook as you already stated.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: ThisWorkbook vs. Module1

    I thought it would confuse the issue unless I just said "Module1" et al, but you show that it can be addressed in a succinct manner

    Well I'll just go on as before. I was just thinking out loud.

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: ThisWorkbook vs. Module1

    Ha, yeah I dont see much benefit unless you are trying to play with events. Although I have had code fire off in the ThisWorkbook area only to call the procedure, which makes the code more modular (which I have found to be ideal).

    This way I can call one module, then another then another... it CAN make things a little easier to audit and track the flow, but not really....

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: ThisWorkbook vs. Module1

    Quote Originally Posted by mikeTRON View Post
    but not really....
    Oohweee! Bonus points for giving a truthful answer!

  6. #6
    Valued Forum Contributor
    Join Date
    11-15-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2003
    Posts
    538

    Re: ThisWorkbook vs. Module1

    Should I mark this solved? Does anyone else have a functional or zen perspective?

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ThisWorkbook vs. Module1

    Don't put code in the ThisWorkbook module, or a worksheet module, unless it's specifically for an event related to the workbook/worksheet.

    If you do put code in either of those types of modules you can run into problems, specifically to do with referencing.
    If posting code please use code tags, see here.

+ 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. Module1 Problem
    By imrvlad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2013, 10:11 AM
  2. Deleting Module1
    By braydon16 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-06-2009, 12:43 PM
  3. Transfer Module1 from PC to Mac
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-05-2006, 01:45 PM
  4. [SOLVED] Install ThisWorkbook and Module1 files with batch script
    By Mark in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2005, 10:05 AM
  5. Module1 vs Thisworkbook for Macro
    By Craigm in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2005, 08:17 AM

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