Forum Discussion

Agus10's avatar
Agus10
Copper Contributor
Jun 23, 2023
Solved

Hiding sheets

I need to create an Excel function to show/hide a sheet, something like this: IF A=5 hide sheet2 Anyway to do this?
  • NikolinoDE's avatar
    Jun 23, 2023

    Agus10 

    For this, only a VBA solution would be appropriate.

    Here are some steps:

    Alt+F11 to open the Visual Basic Editor

    Locate the worksheet where you want to add 5 in A1 (i n file example ist Sheet1).

    Enter the following code:

     

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$1" Then ' Change A1 to the cell you want to monitor
            If Target.Value = 5 Then
                Sheets("Sheet2").Visible = xlSheetHidden ' Replace "Sheet2" with the actual name of the sheet
            Else
                Sheets("Sheet2").Visible = xlSheetVisible ' Replace "Sheet2" with the actual name of the sheet
            End If
        End If
    End Sub

     

    Close the VBA Editor.

     

    Your must use your workbook as a .xlsm file to retain the functionality.

    Example file is included.

     

    I hope that I could help you with this.

Resources