Consulting

Results 1 to 6 of 6

Thread: Solved: Events

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Events

    When including more than one of either the Worksheet_Change, Worksheet_SelectionChange or Worksheet_BeforeDoubleClick event, are there any issues to consider as to whether they are all placed within a single Private Sub or in separate subs? Is there any advantage with one option over the other?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Of course, you place them in the code module of the worksheet that they apply to.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by xld
    Of course, you place them in the code module of the worksheet that they apply to.
    Thanks, but that is not my question. My question is whether it is better to place all the worksheet_beforeafterclick (or worksheet_change or worksheet_selectionchange) scripts in one sub or individual subs. Does it matter if they are all in one or individual subs?

    P.S. I know each type goes in its own sub. I'm asking about groups of like scripts.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    What do you mean by "in one sub"?

    If you are asking whether this:
    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Call DoThis
    Call DoThat
    End Sub
    [/vba]
    or
    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Call DoThis
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
    Call DoThat
    End Sub
    [/vba]
    Then go with the first one as the latter will fail. You cannot have two same-named procedures in the same module.

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by GTO
    What do you mean by "in one sub"?
    Sorry for the confusion.... I mean:

    [vba]
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target as Range)
    'do first BeforeDoubleClick thing here

    'do second BeforeDoubleClick thing here

    'etc., etc.

    [/vba]
    OR

    [vba]
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target as Range)
    'do first BeforeDoubleClick thing here


    [/vba]


    [vba]
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target as Range)
    '
    'do second BeforeDoubleClick thing here

    'etc., etc.

    [/vba]

  6. #6
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by GTO
    Then go with the first one as the latter will fail. You cannot have two same-named procedures in the same module.
    Thanks. I appreciate the help.

Posting Permissions

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