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

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
389
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,214,827
Messages
6,121,817
Members
449,049
Latest member
cybersurfer5000

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