+ Reply to Thread
Results 1 to 6 of 6

Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instances

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-23-2014
    Location
    Fredericia, Denmark
    MS-Off Ver
    Windows Office 2013, Excel 10
    Posts
    139

    Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instances

    Is it possible to program multiple Worksheet Change Events into the same workbook? I have a lengthy set of tasks, each with 2-4 Options. I need to use list boxes (preferably) to select Options used to complete Tasks. Selecting a unique Option for each Task will hide and unhide corresponding bundles of rows in the next sheet titled "Tasks". Please take a look at the attached example. Aks2014 kindly provided me the Worksheet Change code that works for the list box in cell J18. However, I need to do this same function for about 50 tasks, some with differing number of Options, but many with the same number of Options (although each Option and its bundles of rows would be unique to the individual Tasks they reference.) How would I program it so that Sheet1 cell J20 would likewise hide or unhide its corresponding rows in the "Tasks" sheet?

    Note: to expedite your effort I've added the equivalent (partial) coding for this J20 list box in cell A133 in the "Tasks" sheet. Also note that I am ok to use individual macros for each Task and/or Option if that is easier than Worksheet Change Events.

    Thanks for your help. I am so close to getting this figured out but I need more technical expertise....

    (signed)
    The Needy Neophyte
    Attached Files Attached Files

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

    Re: Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instan

    Hi dgibney,

    My personal preference is to keep the Worksheet code as short as possible, and do most of the work in an ordinary code module. This approach has slightly more overhead, but I think it has the advantages of:
    a. Keeping the Worksheet module code cleaner and easier to understand.
    b. It is often easier to update ordinary code modules in workbooks that others are using.
    c. It is easier usually much easier to debug 'Ordinary Code Modules', because even if the routines are not standalone (no formal parameters), they can be tested using small test code modules.

    For example in Sheet1 code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("J18")) Is Nothing Then
          Call Sheet1ChangeEventHandler(Target)
        ElseIf Not Intersect(Target, Range("J20")) Is Nothing Then
          Call Sheet1ChangeEventHandler(Target)
        End If
    
    End Sub


    In an ordinary module:
    Sub TestSheet1ChangeEventHandlerForJ18()
      Sheet1ChangeEventHandler (Sheets("Sheet1").Range("J18"))
    End Sub
    
    Sub TestSheet1ChangeEventHandlerForJ20()
      Sheet1ChangeEventHandler (Sheets("Sheet1").Range("J20"))
    End Sub
    
    Sub Sheet1ChangeEventHandler(ByVal Target As Range)
    
      Dim sAddress As String
      Dim sValue As String
      
      'Get the address of the cell that changed without '$' signs
      sAddress = Target.Address(False, False)
      
      'Get the value in the cell without leading and trailing spaces
      'Allow processing to continue if the cell does not contain text
      On Error Resume Next
      sValue = Trim(Target.Value)
      On Error GoTo 0
    
      'Perform 'J18' only processing
      If sAddress = "J18" Then
        '...
      End If
      
      'Perform 'J20' only processing
      If sAddress = "J20" Then
        '...
      End If
      
      
      'Perform common processing
      '...
    
    End Sub
    Lewis

  3. #3
    Forum Contributor
    Join Date
    05-23-2014
    Location
    Fredericia, Denmark
    MS-Off Ver
    Windows Office 2013, Excel 10
    Posts
    139

    Re: Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instan

    Thanks for the response LJ. Unfortunately I only understood about a third of it, sorry! I tried to create macros with the title "TestSheet1ChangeEventHandlerForJ20()" and "Sheet1ChangeEventHandlerForJ20()" but Excel would not accept them. It would probably be most helpful, and educational, if you could write out the code for on the list boxes and let me know if there is anything special about how are copied into the Worksheet or ordinary module. (I'm still confused a bit by what these acronyms all mean....)

    Thanks again for your response. The (truly) Needy Neophyte...

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

    Re: Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instan

    Hi dgibney,

    Before starting here are a few tips:
    1. To access Visual Basic (VBA) see:
    http://www.ablebits.com/office-addin...a-macro-excel/
    a. Click on any cell in the Excel Spreadsheet (may not be needed).
    b. ALT-F11 to get to VBA.
    c. CTRL-R to get project explorer (if it isn't already showing).
    d. Double Click on a 'Module Name' in 'Project Explorer' to see code for that module.

    2. 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

    3.Using the debugger.
    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.


    4. To import or export VBA code:
    a. To export, right click on the Module Name in the 'Project Explorer'.
    b. Select export file. I suggest you use a SubFolder that only contains exported (.bas) files.
    Keep the original name.
    c. To import, right click anywhere in 'Project Explorer'.
    d. Select import file. Select a file to import.

    ---------------------

    There are many different types of Modules. I am just going to describe the follow 3 module types:
    1. Ordinary Code module. That is where most of the code is usually placed. Ordinary modules have names such as 'Module1' or 'Module2'. In the VBA editor, to create an ordinary code module:
    a. Right Click on any item for the workbook in the 'Project Explorer'. That item will be highlighted.
    b. Select Insert > Module

    Names like Module1 are not very useful. To rename an ordinary VBA module from the VBA Editor:
    a. Press f4 to show the Properties Window.
    b. Change the name of the Module in the Properties Window.

    2. ThisWorkbook Module. 'ThisWorkBook' module contains special code to process Workbook type events, such as Workbook_Open() to do something automatically each time the workbook is opened.

    3. Sheet Module. Sheet modules contain special code to process Sheet type events such as Worksheet_Change(), which is activated each time a user manually changes a value in the sheet or when VBA code changes a value in the sheet.

    -------------

    To copy code, from one source to another you can:
    a. Cut and Paste (just like in a word processor).
    b. For ordinary code modules you can export the code to a file, and then import to a new workbook.
    c. To copy an entire workbook sheet and it's associated code (Code in the Sheet1 module for example),
    copy the sheet to the new workbook manually in Excel.
    d. I don't recommend exporting and then importing ThisWorkbook or Sheet code directly.


    -------------

    Attached is an implementation of my original concept for 'J18' and 'J20'. It is NOT SUITABLE for having 50 options as you mention in your original post, because the design makes it easy to place code in the wrong place. That means each time you make one change you have to test everything. The next post will contain a similar solution that is more compartmentalized and easier to maintain.

    Lewis

    Sheet1 Code:
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("J18")) Is Nothing Then
          Call Sheet1ChangeEventHandler(Target)
        ElseIf Not Intersect(Target, Range("J20")) Is Nothing Then
          Call Sheet1ChangeEventHandler(Target)
        End If
    
    End Sub
    Module ModDisplayOrHideRows code:
    Option Explicit
    
    
    Sub Sheet1ChangeEventHandler(ByVal Target As Range)
    
      Dim sAddress As String
      Dim sValue As String
      
      'Get the address of the cell that changed without '$' signs
      sAddress = Target.Address(False, False)
      
      'Get the value in the cell without leading and trailing spaces
      'Allow processing to continue if the cell does not contain text
      On Error Resume Next
      sValue = Trim(Target.Value)
      On Error GoTo 0
    
      'Perform 'J18' only processing
      If sAddress = "J18" Then
        ' HIDE ROWS IN TASKS WORKSHEET BASED ON USER SELECTION
        If Target.Value = "Not Pursuing" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("105:117").EntireRow.Hidden = True
        ElseIf Target.Value = "Option 1" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("105:113").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("114:116").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("117:117").EntireRow.Hidden = False
        ElseIf Target.Value = "Option 2" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("105:109").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("110:113").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("114:117").EntireRow.Hidden = False
        Else
            'This branch of code should NEVER be selected.
            MsgBox "SOFTWARE INTEGRITY ERROR.  Illegal selection on:" & vbCrLf & _
                   "Sheet 'Tasks'" & vbCrLf & _
                   "Cell: '" & sAddress & "'" & vbCrLf & _
                   "Value: '" & sValue & "'"
            Stop
        End If
      End If
      
      'Perform 'J20' only processing
      If sAddress = "J20" Then
        If Target.Value = "Not Pursuing" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("121:131").EntireRow.Hidden = True
        ElseIf Target.Value = "Option A" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("121:127").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("128:130").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("131:131").EntireRow.Hidden = False
        ElseIf Target.Value = "Option B" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("121:124").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("125:127").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("128:131").EntireRow.Hidden = False
        Else
            'This branch of code should NEVER be selected.
            MsgBox "SOFTWARE INTEGRITY ERROR.  Illegal selection on:" & vbCrLf & _
                   "Sheet 'Tasks'" & vbCrLf & _
                   "Cell: '" & sAddress & "'" & vbCrLf & _
                   "Value: '" & sValue & "'"
            Stop
        End If
      End If
      
    End Sub
    Last edited by LJMetzger; 07-01-2014 at 12:42 PM.

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

    Re: Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instan

    See the attached code for compartmentalizing the code for each cell in Sheet1. I think it is easier to maintain and less prone to mistakes.

    I am not sure how to handle the common instructions, because it is hard for me to picture what the sheet will look like when you have 50 choices instead of only 2. I would leave it alone for now or perhaps just put the 'common instructions' in one place at the top of the worksheet all the time.

    Lewis

    Sheet1 code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("J18")) Is Nothing Then
          Call ProcessSheet1ChangeOnCellJ18(Target)
        ElseIf Not Intersect(Target, Range("J20")) Is Nothing Then
          Call ProcessSheet1ChangeOnCellJ20(Target)
        End If
    
    End Sub

    Module ModDisplayOrHideRows code:
    Option Explicit
    
    
    Sub ProcessSheet1ChangeOnCellJ18(ByVal Target As Range)
    
      Dim sAddress As String
      Dim sValue As String
      
      'Get the address of the cell that changed without '$' signs
      sAddress = Target.Address(False, False)
      
      'Get the value in the cell without leading and trailing spaces
      'Allow processing to continue if the cell does not contain text
      On Error Resume Next
      sValue = Trim(Target.Value)
      On Error GoTo 0
    
      'Perform 'J18' only processing
      If sAddress = "J18" Then
        ' HIDE ROWS IN TASKS WORKSHEET BASED ON USER SELECTION
        If Target.Value = "Not Pursuing" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("105:117").EntireRow.Hidden = True
        ElseIf Target.Value = "Option 1" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("105:113").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("114:116").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("117:117").EntireRow.Hidden = False
        ElseIf Target.Value = "Option 2" Then
            ActiveWorkbook.Sheets("Tasks").Rows("104:104").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("105:109").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("110:113").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("114:117").EntireRow.Hidden = False
        Else
            'This branch of code should NEVER be selected.
            MsgBox "SOFTWARE INTEGRITY ERROR.  Illegal selection on:" & vbCrLf & _
                   "Sheet 'Tasks'" & vbCrLf & _
                   "Cell: '" & sAddress & "'" & vbCrLf & _
                   "Value: '" & sValue & "'"
            Stop
        End If
      End If
        
    End Sub
    
    
    Sub ProcessSheet1ChangeOnCellJ20(ByVal Target As Range)
    
      Dim sAddress As String
      Dim sValue As String
      
      'Get the address of the cell that changed without '$' signs
      sAddress = Target.Address(False, False)
      
      'Get the value in the cell without leading and trailing spaces
      'Allow processing to continue if the cell does not contain text
      On Error Resume Next
      sValue = Trim(Target.Value)
      On Error GoTo 0
    
      
      'Perform 'J20' only processing
      If sAddress = "J20" Then
        If Target.Value = "Not Pursuing" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("121:131").EntireRow.Hidden = True
        ElseIf Target.Value = "Option A" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("121:127").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("128:130").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("131:131").EntireRow.Hidden = False
        ElseIf Target.Value = "Option B" Then
            ActiveWorkbook.Sheets("Tasks").Rows("120:120").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("121:124").EntireRow.Hidden = False
            ActiveWorkbook.Sheets("Tasks").Rows("125:127").EntireRow.Hidden = True
            ActiveWorkbook.Sheets("Tasks").Rows("128:131").EntireRow.Hidden = False
        Else
            'This branch of code should NEVER be selected.
            MsgBox "SOFTWARE INTEGRITY ERROR.  Illegal selection on:" & vbCrLf & _
                   "Sheet 'Tasks'" & vbCrLf & _
                   "Cell: '" & sAddress & "'" & vbCrLf & _
                   "Value: '" & sValue & "'"
            Stop
        End If
      End If
      
    End Sub

  6. #6
    Forum Contributor
    Join Date
    05-23-2014
    Location
    Fredericia, Denmark
    MS-Off Ver
    Windows Office 2013, Excel 10
    Posts
    139

    Smile Re: Multiple Worksheet Change events??? "macro" needed to hide/unhide rows multiple instan

    Lewis, YOU are the man! This works perfectly. And, thanks for sending the educational instructions, they help me to better understand code relationships.

    THANK YOU! THANK YOU! THANK YOU!

    David

+ 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. [SOLVED] Formula Needed to fill multiple cells with "No" when the word "No" is entered into a cell
    By excelteam777 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-09-2013, 05:36 PM
  2. [SOLVED] Click "-" or "+" to hide and unhide rows
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-09-2013, 08:03 AM
  3. Replies: 1
    Last Post: 09-21-2013, 03:18 AM
  4. Hide rows based on value "0" in multiple columns
    By willieh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-18-2012, 06:05 AM
  5. Replies: 2
    Last Post: 03-20-2012, 09:25 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