How to remove error

lcaindoy

New Member
Joined
Jun 12, 2018
Messages
22
Hi,

I'm working on the below code with button. How do I remove error by code when the button for hiding has already been used and then an error will prompt if you click the button again
Sub Hide()
'
' Hide Macro
'
'
Sheets(Array("....", "....", "....", "....", "....", "....")) _
.Select
Sheets("Travel").Activate
ActiveWindow.SelectedSheets.Visible = False
On Error Resume Next
Worksheets("Summary").Activate
End Sub
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
That is because you have already hidden the sheets and now your trying to hide them again.

If your trying to toggle the sheets from visible to hidden you cannot do that with an array.
If you want to toggle the visibility from visible to not visible you need to use a script like this.
In my script I have included 6 sheet names.

Modify sheet names and add more if needed.

My sheet names are:
One
Five
David
Mark
John
Mary




Try this script:

Code:
Sub Toggle_Sheets()
'Modified 6/14/18 12:20 AM EDT
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To Sheets.Count
    With Sheets(i)
        MyVal = Sheets(i).Name
        Select Case MyVal
            Case "One"
              .Visible = Not .Visible
            Case "Five"
                .Visible = Not .Visible
            Case "David"
                .Visible = Not .Visible
            Case "Mark"
              .Visible = Not .Visible
            Case "John"
                .Visible = Not .Visible
            Case "Mary"
                .Visible = Not .Visible
        End Select
    End With
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
That is because you have already hidden the sheets and now your trying to hide them again.

If your trying to toggle the sheets from visible to hidden you cannot do that with an array.
If you want to toggle the visibility from visible to not visible you need to use a script like this.
In my script I have included 6 sheet names.

Modify sheet names and add more if needed.

My sheet names are:
One
Five
David
Mark
John
Mary



Try this script:

Code:
Sub Toggle_Sheets()
'Modified 6/14/18 12:20 AM EDT
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To Sheets.Count
    With Sheets(i)
        MyVal = Sheets(i).Name
        Select Case MyVal
            Case "One"
              .Visible = Not .Visible
            Case "Five"
                .Visible = Not .Visible
            Case "David"
                .Visible = Not .Visible
            Case "Mark"
              .Visible = Not .Visible
            Case "John"
                .Visible = Not .Visible
            Case "Mary"
                .Visible = Not .Visible
        End Select
    End With
    Next
Application.ScreenUpdating = True
End Sub

Thank you for your assistance :)
 
Upvote 0
Alternative suggestion:
Code:
Sub Toggle_Sheets()
    
    Dim i As Long
    Const SHEET_NAMES As String = "One|Five|David|Mark|John|Mary"
    
    Application.ScreenUpdating = False
    
    For i = 1 To Sheets.Count
        With Sheets(i)
            If InStr(SHEET_NAMES, .Name) Then .Visible = Not .Visible
        End With
    Next i

    Sheets("Summary").Select

    Application.ScreenUpdating = True
    
End Sub
Instr returns a non-negative number.
If Instr returns 0, it evaluates as FALSE because the string being searched (const variable SHEET_NAMES) does not contain the sheet name
If the sheet name is found, Instr returns a positive integer which evaluates to TRUE and then toggles the visible property of that sheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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