+ Reply to Thread
Results 1 to 6 of 6

Simplified maintenance for SelectionChange routine in multiple workshee

  1. #1
    Registered User
    Join Date
    09-27-2014
    Location
    New York City
    MS-Off Ver
    2010
    Posts
    32

    Simplified maintenance for SelectionChange routine in multiple workshee

    There are two attached files for this post.
    1. MS Word .docx file describing the problem and the solution
    2. MS Excel .xlsm workbook to demo the solution


    Any suggestions leading to improvement gratefully accepted.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,434

    Re: Simplified maintenance for SelectionChange routine in multiple worksheets

    Probably being stupid here, but why don't you just use the Workbook_SheetSelectionChange Event handler?

    https://msdn.microsoft.com/en-us/lib.../ff837368.aspx
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,434

    Re: Simplified maintenance for SelectionChange routine in multiple worksheets

    Question: do you really want a Selection Change Event handler, or do you need a Change Event handler? Or both?

  4. #4
    Registered User
    Join Date
    09-27-2014
    Location
    New York City
    MS-Off Ver
    2010
    Posts
    32

    Re: Simplified maintenance for SelectionChange routine in multiple worksheets

    Thanks for the replies guys. The workbook contains 30+ worksheets which are identical in function, but unique by an ID number. SelectionChange routine is used to log changes and handle a few control functions. When a new function is added or an old function is enhanced, the Selectionchange in each worksheet has to be changed manually. In addition, as more these worksheets are added to the workbook, the modification task gets longer. I tried to document this in the word document attached to the initial post to this thread and I attached a workbook example of the proposed solution (using Sheet1 as the example and Sheet2 for demonstration purposes). If there were more copies of Sheet1 (i.e., Sheet3, Sheet4,...Sheetnn), it would be come clearer that changes would only need to be made in the external Macro to support all the related worksheets. The worksheets are not changed only the external Macro (called by the Stub in the worksheet) need be modified.

    If there is easier way to do this, I'd appreciate the info.

    Thanks again for the support.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,434

    Re: Simplified maintenance for SelectionChange routine in multiple worksheets

    Guess I wasn't clear enough. In a worksheet class module, you have sheet related events, for example:

    Please Login or Register  to view this content.
    and:

    Please Login or Register  to view this content.
    The Worksheet_SelectionChange will fire when you select a cell or range on the worksheet; the Worksheet_Change will fire when you change a cell or range on the worksheet. Given that you appear to be trying to create a change log, I would have thought that you would require the latter. That said, if you need to record the before status, you could pick that up when the cell is selected.

    These events relate to the specific worksheet in which they are present.

    You can, however, have equivalent event handlers in the workbook class module. For example:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    The Workbook_SheetSelectionChange will fire when you select a cell or range on any worksheet; the Workbook_SheetChange will fire when you change a cell or range on any worksheet.

    These events relate to every worksheet in the workbook.

    Note the subtle difference is that the workbook event handlers have sh as a parameter; this is the worksheet being acted upon.

    My point is that you can use a workbook event handler and, within that, refer to the sheet being acted upon. You do not need to have code behind every worksheet. This is especially true if you want to take the same action regardless of the active sheet. Much cleaner from a maintenance and support perspective.

    You would use sh.Range("..."), for example, to refer to a cell or range on worksheet "sh" rather than ActiveSheet.Range("...")

    Note that it is normal practice to define Public variables in a Standard Module. Then you would not need to prefix references with "ThisWorkbook", for example, ThisWorkbook.EventLogWs and ThisWorkbook.pass ... which, I must admit, I found quite confusing.

  6. #6
    Registered User
    Join Date
    09-27-2014
    Location
    New York City
    MS-Off Ver
    2010
    Posts
    32

    Re: Simplified maintenance for SelectionChange routine in multiple worksheets

    As you have no doubt concluded by now, my experience with Excel is not extensive and this lack of Excel specific experience is reflected in the implementation decisions that I've made. Thank you for your time and your patience in explaining stuff.

    I started out trying to utilize the workbook level event, but there were two reasons that led me to the decision that I made with regard to the level of SelectionChange implementation. The first had to do with the fact that there was another worksheet "type" for which SelectionChange was needed and I felt that the supporting the two functions within the same stream of logic was a level of complexity with which I chose not to deal. The second was that the granular approach was logically cleaner and made expansion of the application more flexibly possible.

    The thing about the Public variables also logical assessment and forced the coding discipline to avoid ambiguity (since I do not yet do EXPLICIT variable definition and reference).

    And as for the "cleaner from a maintenance and support perspective", that is the goal of the tip that I submitted (which combines the efficiency of the workbook level within the environment of the worksheet level function. The stub does not change and the commonality of code is maintained in the macro. It does take two pieces of related code to implement, but does so relatively efficiently. Our real application is a bit more complicated than the Demo .xlsm; the demo is intended to only illustrate the basic idea.

    Thanks again for the help and explanations; they are very helpful.

+ 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. Help to simplified multiple IF condition
    By amein in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-02-2016, 03:29 AM
  2. Handling of multiple selection via SelectionChange
    By Loski in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2015, 02:27 AM
  3. Replies: 0
    Last Post: 09-18-2013, 08:00 AM
  4. Replies: 0
    Last Post: 08-21-2012, 07:53 PM
  5. Simplified Report for Equipment Maintenance
    By Johnald in forum Excel General
    Replies: 5
    Last Post: 01-10-2012, 09:55 PM
  6. Replies: 0
    Last Post: 03-31-2005, 02:06 PM

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