Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to hide/show excel sheet 1

Status
Not open for further replies.

baltog

Programmer
Dec 19, 2002
22
0
0
US
Hi all,

I have 1 workbook that contains 3 worksheets. On load i want to set the 2 worksheets hidden, then on set these visible on certain mouse or keyboard events.

Thanks in advance
 
try this

' Hide Worksheet
For Each sh In Sheets
sh.Visible = False
Next sh

'Gray
 
This will help you..


Sub HideSheet()
Worksheets("Sheet2").Visible = False
Worksheets("Sheet3").Visible = False
End Sub

Sub ShowSheet()
Worksheets("Sheet2").Visible = True
Worksheets("Sheet3").Visible = True
End Sub

For those who don't know how to place a shortcut key

1 Open Excel.
2 Right click on the Sheet name tab.
3 Select View Code from the Pop-up menu
4 Insert a New Module
5 Copy the code and paste it
6 Push Alt+Q to return to Excels normal view.
7 Push Alt+F8 and then select the macro name
8 Push Options and assign a shortcut key.



Gray
 
Can someone take this one step further and advise how I can put this so that it works On the Open Workbook event.

I keep getting run time error 9 subscript out of range

I have put the code in module 1, called it HideSheet and called if from the Open event but it doesn't work!

Very frustrated and any help appreciated.

Thanks
PassingBy

"Happiness is not getting what you want but wanting what you have."
 
Hi,

There are 3 states to the Visible worksheet property

xlSheetVisible
xlSheetHidden
xlSheetVeryHidden

The first 2 can be manipulated by the user from menu selection. The latter can ONLY be changed via VBA or in the VB Editor.

In this instance, Subscript out of range usually indicates a non-existant worksheet name. Make sure that your 2 macros are either in a MODULE or in the WorkBook Object.

Hope this helps :)

Skip,
Skip@TheOfficeExperts.com
 
Skip

The following is located in Module 1

Sub HideSheet()
Worksheets("sheet1").Visible = False
End Sub

I am trying to call HideSheet as follows

Private Sub Workbook_Open()
HideSheet
End Sub

Is this completely wrong?
I would like it to be xlSheetVeryHidden but its not essential.

Thanks
PassingBy


"Happiness is not getting what you want but wanting what you have."
 
Are you sure your worksheet is named "sheet1"? There is no other reason why your code wouldn't work. You can't hide the all worksheets, but the error message when you try to do that is a different one.


Rob
[flowerface]
 
Guys

I confess I was referencing the wrong sheet name - my humble apologies, however, I have now corrected this to "Post codes", closed it all down and reopened.

Now I'm getting error 1004 unable to set the visible property of the worksheet class.

My hair is falling out rapidly!

PassingBy

"Happiness is not getting what you want but wanting what you have."
 
Skip/Rob
I have 2 sheets in the workbook
Post Codes
Input

Post codes is 100% protected
Input partially protected.

Could the protection be stopping the Call from working?
I will try unprotecting and see if the error occurs

PassingBy


"Happiness is not getting what you want but wanting what you have."
 
Thats cracked it!
The workbook was protected as well.

Remove the protection and the sheet became hidden, now all I need to do is re-protect once opened.

Should I only protect the remaining visible sheet or the whole workbook?

PassingBy


"Happiness is not getting what you want but wanting what you have."
 
Thanks for your help guys

The final solution was
Protect the sheets and leave the Workbook unprotected.

It seems that it cannot change the sheet properties on opening if the workbook is protected.

Star for your patience!

PassingBy

"Happiness is not getting what you want but wanting what you have."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top