problem hide the first sheet instead of last sheet & type mismatch

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
Hello
I try desighn macro by hide all of sheets except "sheet2" but the code hide also "sheet2" and keep the last sheet is showed and gives mismatch error in this line
VBA Code:
Sheets(ws).Visible = xlSheetVeryHidden
VBA Code:
Option Explicit
Sub nn()
Dim ws As Worksheet
For Each ws In Sheets
If ws.Name <> "Sheet2" Then
Sheets(ws).Visible = xlSheetVeryHidden
End If
Next
End Sub
How can I fix it please?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi
try

VBA Code:
Option Explicit
Sub nn()
    Dim ws          As Worksheet
    
    With Worksheets("Sheet2")
        .Visible = xlSheetVisible
        For Each ws In Worksheets
            If ws.Name <> .Name Then ws.Visible = xlSheetVeryHidden
        Next
    End With
    
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,215,487
Messages
6,125,079
Members
449,205
Latest member
Healthydogs

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