Forum Discussion

AB_2019's avatar
AB_2019
Copper Contributor
Jul 12, 2019
Solved

Excel Hide Sheets Based on A Cell Value

Hi,   I am trying to create a workbook which hides inactive sheets based on a cell value on the front sheet. i.e if the cell value us "yes" it is visible, if it is "no" it is hidden.   Private Su...
  • IngeborgHawighorst's avatar
    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

     

     

Resources