View/Hide sheets based on sheets' name with InputBox

Daniel2022

New Member
Joined
Feb 20, 2022
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi there. I'm a fairly new in VBA but things used to go well until this moment.
My co-workers use Excel that consist 50 different sheets while this amount rises every month. I managed to create module that creates new sheet with desirable template but I also want to create button that opens and hides required sheet. My idea is to insert InputBox where a person puts sheet' name.
However, I right now I faced an issue that I cannot create similar macros to view and hide sheet in accordance to its name.
I also attach the screenshot how "create" button looks and what it shows and code:
VBA Code:
Sub NewSheet()
    Dim s As String
    s = InputBox("Please write month and year e.g September 2021")
    If s = "" Then Exit Sub
    
    Sheets("Macro").Copy , Sheets(Sheets.Count)
    With ActiveSheet
        .Name = s
    End With
    
End Sub




Any help/ ideas how to do it will be valuable.

Thank you,
Kind Regards,
Daniel


1656506314667.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,
you should be able to do what you want in same code

VBA Code:
Sub NewSheet()
    Dim s As Variant
    Do
        s = InputBox("Please write month And year e.g September 2021", "Enter Sheet Name")
        If StrPtr(s) = 0 Then Exit Sub
    Loop Until IsDate(s)
   
    s = Format(DateValue(s), "mmmm yyyy")
   
    If Not Evaluate("ISREF('" & s & "'!A1)") Then
        'create new sheet
        Worksheets("Macro").Copy , Sheets(Sheets.Count)
        With ActiveSheet
            .Name = s
        End With
       
    Else
        'sheet name exists
        With Worksheets(s)
            .Visible = xlSheetVisible
            .Activate
        End With
       
    End If
   
End Sub

Code checks to see if sheet name exists & will either create new sheet or unhide & activate it.

Based on your post, I have assumed all sheets are named by month / year? Updated code should allow any valid date entry like Feb 2023 to be entered & will change entry to required format

Hope Helpful

Dave
 
Upvote 0
Try:
VBA Code:
Sub View_HideSheet()
    Dim s As String
    s = InputBox("Please enter the name of the sheet to view or hide.")
    If s = "" Then Exit Sub
    If MsgBox("Click 'Yes' to view the sheet or 'No' to hide the sheet.", vbYesNo) = vbYes Then
        Sheets(s).Visible = True
    Else
        Sheets(s).Visible = False
    End If
End Sub
 
Upvote 0
Try:
VBA Code:
Sub View_HideSheet()
    Dim s As String
    s = InputBox("Please enter the name of the sheet to view or hide.")
    If s = "" Then Exit Sub
    If MsgBox("Click 'Yes' to view the sheet or 'No' to hide the sheet.", vbYesNo) = vbYes Then
        Sheets(s).Visible = True
    Else
        Sheets(s).Visible = False
    End If
End Sub

Thanks a lot!
It does work. I'll try to split it by 2 buttons- view/hide.
 
Upvote 0
Hi Daniel,

one idea might be to draw an activeX box on your (main?) sheet ie. one that is not getting hidden. Then use the button as a "Toggle" ie. if a sheet is hidden, it will become visible, and vice versa.

Try the below code (which is inserted into the actual button itself - right click on the button you've drawn when in "Design Mode" on the developer tab, and select "view code".

It will check if the sheet exists first, then check its status, and toggle it on or off depending on its status.

Cheers
Rob

VBA Code:
Private Sub CommandButton1_Click()

Dim s As String
s = InputBox("Please write month and year e.g September 2021")
    If s = "" Then Exit Sub
    
        If SheetExists(s) Then
            If (Sheets(s).Visible = False) Then
                Sheets(s).Visible = True
            Else
                Sheets(s).Visible = False
            End If
        Else
            MsgBox ("Worksheet Does not Exist")
        End If
    
End Sub

Private Function SheetExists(Tabname) As Boolean
'   Returns logical TRUE if sheet exists in the active workbook
    Dim x As Object
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(Tabname)
    If Err = 0 Then SheetExists = True _
        Else SheetExists = False
End Function
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top