Solved

Hiding all tabs with VBA when workbook loads

Posted on 2013-12-24
11
338 Views
Last Modified: 2013-12-25
Folks,
I know I can go to Options - Advanced - Display Options for This Workbook and Uncheck "Show sheet Tabs". However, that can be changed by the user. Is there's a VBA way to keep them from doing that once a workbook has been protected? I found some code that I'm looking at but I'm not sure this will meet my objective.

Sub HideTabs()
Dim w As Worksheet
Set w = Worksheets.Add
w.Name = "ListOfSheets"
For x = 1 To Worksheets.Count
    w.Cells(x, 1) = Sheets(x).Name
Next

wSheets = Array("SHEET1", "SHEET2", "SHEET3")
wsstat = Worksheets(wSheets(0)).Visible
If wsstat = xlSheetVisible Then
wsstat = xlSheetHidden
Else
wsstat = xlSheetVisible
End If
For n = 0 To UBound(wSheets)
Worksheets(wSheets(n)).Visible = wsstat
Next n
End Sub

Open in new window

0
Comment
Question by:Frank Freese
  • 6
  • 5
11 Comments
 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39738655
The code you have does make the tabs individually hidden and it would require code to unhide them. If you set the Visible attribute to False this applies to the tab. The setting you are referring to actually hides the entire tab bar. This does not affect the individual tabs properties.
0
 

Author Comment

by:Frank Freese
ID: 39738666
This is the old ORGINAL code:

w.Name = "ListOfSheets"
For x = 1 To Worksheets.Count
    w.Cells(x, 1) = Sheets(x).Name
Next

wSheets = Array("SHEET1", "SHEET2", "SHEET3")

Open in new window


Here's my new code:
For x = 1 to Worksheets.Count
w.Cell(x)=Sheets(x)
Next

Open in new window


In my ORGINAL code I'd remove line 4 and replace 5 - 7 with my new code. But will it work?
0
 

Author Comment

by:Frank Freese
ID: 39738777
Folks,
I tried the code below in the Activate section of the workbook so when the workbook is activated it would hide all tabs. It crashed at line 6 with the following error:

 "Run-time error '1004'
Application-defined or object defined error"

Dim intIndex As Integer
Dim w As Worksheet
Set w = Worksheets.Add

For x = 1 To Worksheets.Count
    w.Cells(x, 1) = Sheets(x)
Next

For x = 1 To Sheets(x)
wSheets = Array(Sheets(x))
wsstat = Worksheets(wSheets(0)).Visible
Next x

If wsstat = xlSheetVisible Then
wsstat = xlSheetHidden
Else
wsstat = xlSheetVisible
End If
For n = 0 To UBound(wSheets)
Worksheets(wSheets(n)).Visible = wsstat
Next n

Open in new window


Again, my objective is that when a workbook is activated all tabs are hidden.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39738780
put it in the Workbook_Open section instead.
0
 

Author Comment

by:Frank Freese
ID: 39738783
My error.
It is in the Workbook_Open section
0
 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39738788
what are you trying to do in lines 5-7?
I am dense and cannot follow
0
 
LVL 11

Accepted Solution

by:
Gregory Miller earned 500 total points
ID: 39738809
OK, this works like a champ...

Private Sub Workbook_Open()

For x = 1 To Sheets.Count
    If ThisWorkbook.Sheets(x).Name <> "Sheet1" Then
        ThisWorkbook.Sheets(x).Visible = False
    End If
Next

End Sub

Open in new window


Modify line 4 with the sheet name that you want to remain visible. You have to have one sheet visible.
0
 

Author Comment

by:Frank Freese
ID: 39739133
Merry Christmas to you...
At the beginning of this thread I was looking for a way to hide the worksheet tab. It is my fault if I took you in the wrong direction since then and I apologize. It is not the worksheets I'm neding to hide, simple their tabs.
0
 

Author Comment

by:Frank Freese
ID: 39739146
You're not going to believe this but all I need to do is in a module;

ActiveWindow.DisplayWorkbookTabs = False

I still have the need to hide sheets later so you've earned that.

Thanks
0
 

Author Closing Comment

by:Frank Freese
ID: 39739149
thank you
enjoy the holidays
0
 
LVL 11

Expert Comment

by:Gregory Miller
ID: 39739158
Thanks, you too!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question