Hi there,
First time poster, and beginner with VBA. I'm trying to write a code to hide/unhide rows based upon multiple criteria. I've managed to fumble my way through worksheet change events but can only get them to fire based on the last cell changed. Ultimately this is what I want to do, but I need the macro to evaluate all cells within the relevant range before deciding which rows to hide / unhide. My data is arranged as follows:
Range B8:V8 - Company name
Range B14: V14 - Accounting package (chosen from a validated drop down list)
If any cell in the range B14:V14 includes "Xero" I want the macro to unhide row 15. Conversely, if no cell in the range B14:V14 includes "Xero", I want row 15 to remain hidden.
I'm using a worksheet change event so the worksheet automatically triggers the hide/unhide action but it's not evaluating all results in B14:V14 hence the following conflict arises:
1. Cell B14 - if I select "Xero" in this cell, row 15 unhides which is the desired outcome; but
2 Cell C14 - if I select "MYOB" in this cell, row 15 then hides again which is not the aim (should remain unhidden based on B14 = "Xero")
I also tried creating a calculated flag at W14 to trigger a Worksheet_Calculate event which seemed to work fine, but when I added addition Worksheet_Calculate events the workbook crashed, I guess because it was triggering several macros at once.
Any help will be greatly appreciated. My code is below.
Cheers,
Harry
Private Sub Worksheet_Change(ByVal target As Range)
If target.Count > 1 Then Exit Sub
If Not Intersect(target, Range("B14:V14")) Is Nothing Then
Application.ScreenUpdating = False
Rows("15:15").EntireRow.Hidden = True
'To unhide rows 15:15 based upon selected criteria
Select Case target.Value
Case "Xero"
Rows("15:15").EntireRow.Hidden = False
Case Else
'Rows 15:15 remain hidden
End Select
Application.ScreenUpdating = True
End If
End Sub
First time poster, and beginner with VBA. I'm trying to write a code to hide/unhide rows based upon multiple criteria. I've managed to fumble my way through worksheet change events but can only get them to fire based on the last cell changed. Ultimately this is what I want to do, but I need the macro to evaluate all cells within the relevant range before deciding which rows to hide / unhide. My data is arranged as follows:
Range B8:V8 - Company name
Range B14: V14 - Accounting package (chosen from a validated drop down list)
If any cell in the range B14:V14 includes "Xero" I want the macro to unhide row 15. Conversely, if no cell in the range B14:V14 includes "Xero", I want row 15 to remain hidden.
I'm using a worksheet change event so the worksheet automatically triggers the hide/unhide action but it's not evaluating all results in B14:V14 hence the following conflict arises:
1. Cell B14 - if I select "Xero" in this cell, row 15 unhides which is the desired outcome; but
2 Cell C14 - if I select "MYOB" in this cell, row 15 then hides again which is not the aim (should remain unhidden based on B14 = "Xero")
I also tried creating a calculated flag at W14 to trigger a Worksheet_Calculate event which seemed to work fine, but when I added addition Worksheet_Calculate events the workbook crashed, I guess because it was triggering several macros at once.
Any help will be greatly appreciated. My code is below.
Cheers,
Harry
Private Sub Worksheet_Change(ByVal target As Range)
If target.Count > 1 Then Exit Sub
If Not Intersect(target, Range("B14:V14")) Is Nothing Then
Application.ScreenUpdating = False
Rows("15:15").EntireRow.Hidden = True
'To unhide rows 15:15 based upon selected criteria
Select Case target.Value
Case "Xero"
Rows("15:15").EntireRow.Hidden = False
Case Else
'Rows 15:15 remain hidden
End Select
Application.ScreenUpdating = True
End If
End Sub
Last edited: