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
Bookmarks