+ Reply to Thread
Results 1 to 6 of 6

Difference between sheet, module and thisworkbook in excel

  1. #1
    Registered User
    Join Date
    05-31-2017
    Location
    IRL
    MS-Off Ver
    No idea
    Posts
    12

    Difference between sheet, module and thisworkbook in excel

    I don't want to duplicate code in sheets, so right now i have


    Please Login or Register  to view this content.
    callThisSub works
    callThisSub2 doesn't work, do I need to define it differently? the code is workbook specifik so I guess I should have it in thisworkbook and not in a module?
    Last edited by elfor; 06-01-2017 at 08:51 AM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Difference between sheet, module and thisworkbook in excel

    Treat the code in a worksheet class module to be specific to that worksheet, and is only called from procedures in the same worksheet module - same for code in ThisWorkbook.

    All common code goes in a standard code module.

    And, as posted, your code will not compile. You don't declare CallThisSub & CallThisSub2 as a Sub or Function.

    When you do correct that, it still won't compile but let's not overcomplicate things. One step at a time.
    Last edited by cytop; 06-01-2017 at 09:18 AM.

  3. #3
    Registered User
    Join Date
    05-31-2017
    Location
    IRL
    MS-Off Ver
    No idea
    Posts
    12

    Re: Difference between sheet, module and thisworkbook in excel

    Sub or function not defined for WorkBookTest

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Difference between sheet, module and thisworkbook in excel

    That's right - now your problem is the syntax you use to call the subs.

    If a Sub takes parameters and you bracket the parameter in a call to the Sub then you must use the CALL keyword.
    Please Login or Register  to view this content.
    If you don't bracket the parameters then you can simply call the procedure in-line without the brackets
    Please Login or Register  to view this content.
    It is suggested you do not have Public Subs/Functions in what can be seen as a 'transient' module. The ThisWorkbook class is never going to disappear, but what if you have a procedure in a worksheet class and that worksheet is deleted later? The procedure disappears.

    After saying that, you can call a procedure in the THisWorkbook class (or any sheet class module) by prefacing the procedure name with the container name
    Please Login or Register  to view this content.
    However, you will hit problems at some stage if you do that...

  5. #5
    Registered User
    Join Date
    05-31-2017
    Location
    IRL
    MS-Off Ver
    No idea
    Posts
    12

    Re: Difference between sheet, module and thisworkbook in excel

    If i understand you correctly module1 is a transient module and I should use thisworkbook

    Please Login or Register  to view this content.
    If i then understand you correctly, I can call the workbook directly "ThisWorkBook.WorkBookTest "Hi" but that is not recommended as it can cause problems.

    Though Call or inline still gives me the same error, i probably misinterpreted your text?
    Please Login or Register  to view this content.
    Last edited by elfor; 06-01-2017 at 09:49 AM.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Difference between sheet, module and thisworkbook in excel

    Please Login or Register  to view this content.
    But you should treat ThisWorkbook the same as the individual sheets. Unless the code is only called from within the ThisWorkbook class (or is code for an event procedure) then it should go in a standard code module

+ 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. ThisWorkbook Module Add-in
    By asalamun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-27-2017, 01:04 PM
  2. [SOLVED] Can't reference ThisWorkbook from a module in another workbook module
    By Bukovnik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2015, 11:32 AM
  3. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  4. Excel 2003 "Compile error in hidden module: ThisWorkbook"
    By JeffBrown in forum Excel General
    Replies: 1
    Last Post: 11-05-2007, 04:02 PM
  5. Difference among Module, SheetX, ThisWorkbook?
    By muster in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2006, 02:00 PM
  6. Calls from sheet module to ThisWorkbook module
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2005, 11:05 AM
  7. Sheet turns into a "ThisWorkbook" module
    By Rob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-15-2005, 10:06 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