Hideing Sheet Tabs

Avatar

Board Regular
Joined
Sep 20, 2005
Messages
193
Greetings,

Can someone tell me why this hides all sheet tabs on all open workbooks instead of just the active one please? I'll be damned if i can see why it's doing it..

Set aWorkbook = ActiveWorkbook

counterstop = 1
Do Until counterstop = Sheets.Count + 1
aWorkbook.Sheets(counterstop).Visible = False
counterstop = counterstop + 1
Loop
 
Be careful when you use ActiveWorkbook. If you are closing workbooks the ActiveWorkbook will change and your code might not be working on the book you assume it is.

At the start of your code, put your ActiveWorkbook into a variable or use ThisWorkbook if the code should only be applied to the workbook it is placed in.

Anyway, here is a bit more modular approach. Try this out:

Code:
Sub TestItHide()
   'Place this line in your code to hide.  "Sheet1" is your splash sheet
   VeryHideAllButSplashSheet ThisWorkbook, "Sheet1"
End Sub

Sub TestItUnHide()
   'Place this line in your code to unhide sheets
   UnhideAllSheets ThisWorkbook
End Sub

Public Sub UnhideAllSheets(wb As Workbook)
   Dim sh As Worksheet
   
   For Each sh In wb.Worksheets
      sh.Visible = xlSheetVisible
   Next
End Sub

Public Sub VeryHideAllButSplashSheet(wb As Workbook, splash As String)
   Dim sh As Worksheet
   
   On Error Resume Next
   wb.Worksheets(splash).Activate
   If Err <> 0 Then
      MsgBox "What do you want to do when the splash sheet does not exist?"
      Exit Sub
   End If
   On Error GoTo 0
   ' Splash sheet is active but it might be hidden.  Must be unhidden to hide all others
   wb.Worksheets(splash).Visible = xlSheetVisible
   
   For Each sh In wb.Worksheets
      If sh.Name <> splash Then
         sh.Visible = xlVeryHidden
      End If
   Next
End Sub
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

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