Consulting

Results 1 to 3 of 3

Thread: Where does code go

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location

    Where does code go

    I often read posts where people ask "where does the code go". Could someone please give me a "Code Placement 101" explanation?

    For example, in aoc's thread "Adding Date with Double Click", Simon reinforced the point that the code needed to go on the Worksheet module not a standard module.

    Am I correct in assuming then that any code which is placed in the workbook module is available to the workbook, that code placed in a worksheet module is limited to that sheet, and that code placed in a standard module available to the user in any sheet?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Aussiebear
    ...Am I correct in assuming then that any code which is placed in the workbook module is available to the workbook, that code placed in a worksheet module is limited to that sheet, and that code placed in a standard module available to the user in any sheet?
    Yes, if you see (for example) [vba]Private Sub Workbook_SheetActivate(ByVal Sh As Object)[/vba]then that is workbook event code and it goes in the ThisWorkbook code module, the event code is then run every time any sheet is activated (i.e. worksheet or chartsheet), whereas if you see [vba]Private Sub Worksheet_Activate()[/vba]this is worksheet event code that goes into the code module of the sheet or sheets that you want it to apply to when that particular worksheet (or sheets) is activated...

    For more, see here, here, and here
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you are writing workbook event code, it has to go in ThisWorkbook, otherwise it will not be triggered.

    If you are writing worksheet event code, it has to go in the appropriate worksheet code module, otherwise it will not be triggered.

    If you are writing userform event code, it has to go in userform code module, otherwise it will not be triggered.

    Of course, it gets more difficult if you encapsulate some of the code within those events within a separate procedure. That procedure could go in the same code module, and stay private to that code module, or it could go n a general code module, and be made public to the whole project. The former has the advantage that all of the class properties (such as Me) are still exposed to the procedure, but it can obscure the code readability. The latter has the advantage that it can abstract code, making it simpler to test in isolation, keep the class module code very event specific, but does lose those class properties.

    And it gets even more complex if you create object classes to deal with specific events, such as a form entry class that handles input on all textbox input on a form. This object class would be separate to the form class, but is inextricaly linked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •