Forum Discussion
Excel Hide Sheets Based on A Cell Value
- Jul 12, 2019
AB_2019 If the sheet names are tied to the row numbers, you can use the code below. It will only run when a cell in the range A9 to A258 is changed. It also works if you copy and paste "Yes" or another value into more than one cell at a time. (Note that this is case sensitive, so "yes" will hide the sheet and only "Yes" will show it)
Let me know how you get on with this.
Private Sub Worksheet_Change(ByVal Target As Range) Dim myRow As Integer Dim mySheetName As String Dim cel As Range ' watch only cells A9 to A258 If Not Intersect(Target, Range("A9:A258")) Is Nothing Then ' if more than one cell has been changed, we need to cath them all For Each cel In Target ' get the row of the changed cell ' subtract 8 to arrive at the sheet number myRow = cel.Row - 8 ' build the sheet name mySheetName = "VAR " & Format(myRow, "000") If cel = "Yes" Then ThisWorkbook.Worksheets(mySheetName).Visible = True Else ThisWorkbook.Worksheets(mySheetName).Visible = False End If Next cel End If
AB_2019 If the sheet names are tied to the row numbers, you can use the code below. It will only run when a cell in the range A9 to A258 is changed. It also works if you copy and paste "Yes" or another value into more than one cell at a time. (Note that this is case sensitive, so "yes" will hide the sheet and only "Yes" will show it)
Let me know how you get on with this.
Private Sub Worksheet_Change(ByVal Target As Range) Dim myRow As Integer Dim mySheetName As String Dim cel As Range ' watch only cells A9 to A258 If Not Intersect(Target, Range("A9:A258")) Is Nothing Then ' if more than one cell has been changed, we need to cath them all For Each cel In Target ' get the row of the changed cell ' subtract 8 to arrive at the sheet number myRow = cel.Row - 8 ' build the sheet name mySheetName = "VAR " & Format(myRow, "000") If cel = "Yes" Then ThisWorkbook.Worksheets(mySheetName).Visible = True Else ThisWorkbook.Worksheets(mySheetName).Visible = False End If Next cel End If
AB_2019 Glad this worked for you. It would be great if you could mark my answer as the solution.
Hiding rows is a different question. I'll put that into a new question for you, so it gets more attention.