My Macro is failing w/ seemingly no explanation. I'm thinking it could be a syntactical issue.
It is designed to hide/show certain rows based on a certain cell's (FacilityChoice) value. The value is input via Data Validation List.
It will work for a while , but sometimes the Debugger highlights one of my first two lines. I have been able to fix this simply by deleting and replacing the final character in each line.
Other times the Macro simply does not work & the debugger offers no feedback.
As described above, the failures I have observed fall into two categories:
- an issue with Sub definition or the initial if not statement
- an issue where no feedback is offered by the debugger
In scenario 1, I was able to fix the problem simply by deleting/rekeying the final character in the line.
I have been unable to successfully address scenario 2.
Please see code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("FacilityChoice"), Range(Target.Address)) Is Nothing Then
'Gen Office Scenario
If ActiveWorkbook.Sheets("SNA Tool").Range("FacilityChoice").Value = ActiveWorkbook.Sheets("References").Range("E2") Then
ActiveSheet.Rows(17 & ":" & 23).EntireRow.Hidden = False
ActiveSheet.Rows(25).EntireRow.Hidden = False
ActiveSheet.Rows(29 & ":" & 30).EntireRow.Hidden = False
ActiveSheet.Rows(37 & ":" & 39).EntireRow.Hidden = False
ActiveSheet.Rows(43 & ":" & 45).EntireRow.Hidden = False
'POP Scenario
ElseIf ActiveWorkbook.Sheets("SNA Tool").Range("FacilityChoice").Value = ActiveWorkbook.Sheets("References").Range("E3") Then
ActiveSheet.Rows(17 & ":" & 23).EntireRow.Hidden = True
ActiveSheet.Rows(25).EntireRow.Hidden = True
ActiveSheet.Rows(29 & ":" & 30).EntireRow.Hidden = True
ActiveSheet.Rows(37 & ":" & 39).EntireRow.Hidden = True
ActiveSheet.Rows(43 & ":" & 45).EntireRow.Hidden = True
'Warehouse Scenario
ElseIf ActiveWorkbook.Sheets("SNA Tool").Range("FacilityChoice").Value = ActiveWorkbook.Sheets("References").Range("E4") Then
ActiveSheet.Rows(17 & ":" & 23).EntireRow.Hidden = True
ActiveSheet.Rows(25).EntireRow.Hidden = True
ActiveSheet.Rows(29 & ":" & 30).EntireRow.Hidden = True
ActiveSheet.Rows(37 & ":" & 39).EntireRow.Hidden = True
ActiveSheet.Rows(43 & ":" & 45).EntireRow.Hidden = True
'Rapid Scenario
ElseIf ActiveWorkbook.Sheets("SNA Tool").Range("FacilityChoice").Value = ActiveWorkbook.Sheets("References").Range("E5") Then
ActiveSheet.Rows(17 & ":" & 23).EntireRow.Hidden = True
ActiveSheet.Rows(25).EntireRow.Hidden = True
ActiveSheet.Rows(29 & ":" & 30).EntireRow.Hidden = True
ActiveSheet.Rows(37 & ":" & 39).EntireRow.Hidden = True
ActiveSheet.Rows(43 & ":" & 45).EntireRow.Hidden = True
End If
End If
End Sub
CodePudding user response:
You can use Select Case
and a little re-arrangement to simplify your code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsRef As Worksheet, bHide As Boolean
If Application.Intersect(Me.Range("FacilityChoice"), Target) Is Nothing Then Exit Sub 'no action
Set wsRef = ThisWorkbook.Worksheets("References")
Select Case Me.Range("FacilityChoice").Value
Case wsRef.Range("E2").Value
bHide = False
Case wsRef.Range("E3").Value, wsRef.Range("E4").Value, wsRef.Range("E5").Value
bHide = True
Case Else
Exit Sub 'no action...
End Select
'set row visibility
Me.Range("A17:A23,A25,A29:A30,A37:A39,A43:A45").EntireRow.Hidden = bHide
End Sub
Note in a worksheet event handler you can use Me
to refer to the worksheet: it's safer than using ActiveSheet