Hide sheets

kclong

Board Regular
Joined
Nov 22, 2006
Messages
80
I saw this on the message board, which works to hide all sheets but Sheet1. How do I modify it to hide all sheets except Sheet 1 and Sheet2?

Dim sht As Object

For Each sht In Sheets
If sht.Name <> "Sheet1" Then
sht.Visible = xlSheetHidden
End If
Next sht
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this code, I think it is a little better than what you have and will keep Sheet1 (or any other sheet). Note you can either choose hidden, or very hidden - I typically use very hidden so the user can't get to the sheet except in the VBA editor.

Dim wksCurrent As Worksheet
Const conKeepSheet As String = "Sheet1"

For Each wksCurrent In Worksheets
If wksCurrent.Name <> conKeepSheet Then
wksCurrent.Visible = xlSheetVeryHidden
End If
Next
 
Upvote 0
won't this hide all sheets except sheet1? I'd like to be able to see sheet1 and sheet2.
 
Upvote 0
Just change the following for keeping two sheets.

Const conKeepSheet1 As String = "Sheet1"
Const conKeepSheet2 As String = "Sheet2"

If wksCurrent.Name <> conKeepSheet1 or wksCurrent.Name <> conKeepSheet2 Then
 
Upvote 0
Just change the following for the keeping two sheets.

Const conKeepSheet1 As String = "Sheet1"
Const conKeepSheet2 As String = "Sheet2"

If wksCurrent.Name <> conKeepSheet1 or wksCurrent.Name <> conKeepSheet2 Then

Note it is better to have the sheet names at the top of your code as constants to make them easy to find and so you would only have to change them once if the names of the sheets changed.
 
Last edited:
Upvote 0
Hi All,

First post! Apologies for the NecroThread!

Very new to VBA & I'm trying to create a macros for pretty much the same thing as above, however I need to keep three sheets open - see code below - It throws up an error at the"wksCurrent.Visible = xlSheetVeryHidden" line. ("Run-time error '1004': Method 'Visible' of object '_Worksheet' failed")

Have I left something off?

Thanks in advance

BK

Code:

Sub Shutdown_01()
Dim wksCurrent As Worksheet
Const conKeepSheet1 As String = "Approx POs"
Const conKeepSheet2 As String = "BQ"
Const conKeepSheet3 As String = "Commercials"

For Each wksCurrent In Worksheets
If wksCurrent.Name <> conKeepSheet1 Or wksCurrent.Name <> conKeepSheet2 Or wksCurrent.Name <> conKeepSheet3 Then
wksCurrent.Visible = xlSheetVeryHidden
End If
Next

Sheets("Commercials").Activate 'To Activate
Sheets("Commercials").Range("I27").Activate 'Activate Cell


End Sub
 
Upvote 0
Possibly you need to qualify which worksheets you are referring to, try changing it to
Code:
[COLOR=#333333]For Each wksCurrent In [/COLOR]ThisWorkbook.Worksheets
 
Upvote 0
Hi welcome to forum,

Try this code & see if does what you want:


Code:
Sub Shutdown_01()
    Dim wksCurrent As Worksheet
    Dim m As Variant

    For Each wksCurrent In ThisWorkBook.Worksheets
        M = Application.Match(wksCurrent.Name, Array("Approx POs", "BQ", "Commercials"), False)
        wksCurrent.Visible = IIf(IsError(M), xlSheetVeryHidden, xlSheetVisible)
    Next wksCurrent
    
     With ThisWorkBook.WorkSheets("Commercials")
        .Activate 'To Activate
        .Range("I27").Activate 'Activate Cell
     End With


End Sub





A tip for future, rather than step in to an old thread, start your own & provide a link to any relevant thread. This way, you are likely to get more responses.

Hope helpful

Dave
 
Last edited:
Upvote 0
Hi welcome to forum,

Try this code & see if does what you want:


Code:
Sub Shutdown_01()
    Dim wksCurrent As Worksheet
    Dim m As Variant

    For Each wksCurrent In ThisWorkBook.Worksheets
        M = Application.Match(wksCurrent.Name, Array("Approx POs", "BQ", "Commercials"), False)
        wksCurrent.Visible = IIf(IsError(M), xlSheetVeryHidden, xlSheetVisible)
    Next wksCurrent
    
     With ThisWorkBook.WorkSheets("Commercials")
        .Activate 'To Activate
        .Range("I27").Activate 'Activate Cell
     End With


End Sub





A tip for future, rather than step in to an old thread, start your own & provide a link to any relevant thread. This way, you are likely to get more responses.

Hope helpful

Dave


Dave,

This worked beautifully - Thank you.

Noted re starting new thread.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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