?
Solved

Hiding all tabs with VBA when workbook loads

Posted on 2013-12-24
11
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

719 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