+ Reply to Thread
Results 1 to 11 of 11

Module vs Object

  1. #1
    Registered User
    Join Date
    03-31-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Module vs Object

    Good Day:

    I am new to Excel. Hope someone may answer me, what is the different to have my Excel VBA codes in "Module" vs "The Workbook" which both can be found in the Project Window located on the top left corner by default.

    Please reply with the easiest code, exp: Msgbox "Hi"

    Thanks!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Module vs Object

    Welcome to the forum.

    The Sheet and ThisWorkbook modules are class modules intended to contain code to respond to sheet and workbook events. Events for these modules are predefined by Excel. Code other than that (e.g., macros and user-defined functions) that should be in code modules. To create a code module, right-click on the workbook project or any of its modules, and choose Insert > Module.

    The fourth type of module is a class module, which allow you to create your own classes. Events for these modules are defined in the body of the module. To insert one, right click as above, Insert > Class Module
    Last edited by shg; 03-31-2009 at 04:25 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-31-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Module vs Object

    Good Day Shg:

    Many thanks for the response.

    I'm still confuse. May you please explain using the simplest example: if I were to use the Msgbox, for example:
    Please Login or Register  to view this content.
    what is the different to add it in The Workbook compare with in The Module?

    Thanks.
    Last edited by shg; 03-31-2009 at 04:29 PM. Reason: add code tags

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Module vs Object

    It doesn't process a workbook event, so it goes in a code module.

    Please take a few minutes to read the forum rules, then edit your post to add code tags.

  5. #5
    Registered User
    Join Date
    03-31-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Module vs Object

    Tx Shg.

    May someone please kindly reply my initial question:

    what is the different to have my Excel VBA codes in "Module" vs "The Workbook" which both can be found in the Project Window located on the top left corner by default.

    May the explanation will be using the simplest example: if I were to use the Msgbox of:
    Please Login or Register  to view this content.
    what is the different to add it in The Workbook compare with in The Module?




    Now I think I got another problem, accordingly to Shg, I shall change me flag. I read the rule but don get what I suppose to change. I am to study. Hope someone can give me the most direct way to change me flag.....ok for those who are new like me....just click the yellow icon on the top (after the bold, italic....)...hope this is what shg is referring.

    Thanks.
    Last edited by shg; 03-31-2009 at 04:29 PM. Reason: change quote tags to code tags

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Module vs Object

    Please read the Forum Rules, and then edit both of your posts to wrap your code with Code Tags.

  7. #7
    Registered User
    Join Date
    03-31-2009
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Module vs Object

    May someone please kindly reply my initial question:

    what is the different to have my Excel VBA codes in "Module" vs "The Workbook" which both can be found in the Project Window located on the top left corner by default.

    May the explanation will be using the simplest example: if I were to use the Msgbox of:

    Please Login or Register  to view this content.
    what is the different to add it in The Workbook compare with in The Module?

    Now I think I got another problem, accordingly to Shg, I shall change me flag. I read the rule but don get what I suppose to change. I am to study. Hope someone can give me the most direct way to change me flag.....ok for those who are new like me....just click the yellow icon on the top (after the bold, italic....)...hope this is what shg is referring.

    remark: thanks to those who are sincerely help by providing direct answer and not attempting to show off by talking "cool" yet not helping.
    Last edited by shg; 03-31-2009 at 05:11 PM. Reason: change quote tags to code tags

  8. #8
    Registered User
    Join Date
    07-19-2020
    Location
    Lagos, Nigeria
    MS-Off Ver
    2019
    Posts
    2

    Re: Module vs Object

    Carefully go through this website: excelerator.solutions/vba-starter-kit/objects-modules-class-mods-form/

  9. #9
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Module vs Object

    Hi silver.sagi,

    Your specifc code below will work in any Code Module, HOWEVER
    Please Login or Register  to view this content.
    as shg expained above the Sheet Code Modules and the ThisWorkbook Code module are SPECIAL code modules. Not all code will work inside them.

    Your Sub x() belongs in an Ordinary Code Module such as 'Module1'.

    To add a module in the VBA Editor:
    a. 'Left Click' on any cell in the Excel Spreadsheet.
    b. ALT-F11 to get to VBA.
    c. CTRL-R to get project explorer (if it isn't already showing).
    d. 'Right Click' on the Project or on any module in 'Project Explorer'.
    e. Insert > Module
    f. To rename the Module press 'F4' to access the 'Properties' Window (if the 'Properties' Window is not already visible)
    g. To the right of the (Name) Property, type in the New Name. The name MUST be unique and VBA must be DORMANT (i.e. no Macros running).

    To delete a module in the VBA Editor:
    a. 'Left Click' on any cell in the Excel Spreadsheet.
    b. ALT-F11 to get to VBA.
    c. CTRL-R to get project explorer (if it isn't already showing).
    d. 'Right Click' the module to be Deleted.
    e. 'Left Clock' Remove ...
    f. Select 'No' when asked 'Do you want to export ...'.

    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    To manually set a breakpoint, see http://www.wiseowl.co.uk/blog/s196/breakpoints.htm

    Lewis

  10. #10
    Registered User
    Join Date
    03-17-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Module vs Object

    Marked, thank you!

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    79,369

    Re: Module vs Object

    Why do you keep posting this message in threads that are not yours?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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