Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Issue on Close Q

I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Issue on Close Q

add the top line

On Error Resume Next
Dim sh As Worksheet
For Each sh In Worksheets

If sh.Name = "splash" Then
sh.Visible = True
Else
sh.Visible = xlVeryHidden
End If
Next

"Sean" wrote:

I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Issue on Close Q

First, you haven't declared the variable 'ws'. Putting "Option Explicit" as
the very first line in the code module will require to explicitly declare
your variable. Do this, always.

You code will blow up if you don't have a sheet named "Splash" (test the
sheet in case it contains leading/trailing spaces, a misspelled name, etc).
If there is no sheet named "Splash" your code will attempt to set
xlVeryHidden on every sheet. A workbook must always have at least one
visible sheet. You'll blow up on the last sheet.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Sean" wrote in message
oups.com...
I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Issue on Close Q

And since one sheet must be visible, you might as make sure it's Splash:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet 'not worksheets
me.worksheets("splash").visible = xlsheetvisible
For Each ws In me.Worksheets
If lcase(ws.Name) = lcase("Splash") Then
'do nothing, already taken care of
Else
ws.Visible = xlVeryHidden
End If
Next ws
End Sub

Remember that the workbook still has to be saved for this to work
effectively--if the user closes without saving, all your work is to naught.



Sean wrote:

I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 454
Default Issue on Close Q

Thanks Guys, Fixed


Dave Peterson wrote:
And since one sheet must be visible, you might as make sure it's Splash:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet 'not worksheets
me.worksheets("splash").visible = xlsheetvisible
For Each ws In me.Worksheets
If lcase(ws.Name) = lcase("Splash") Then
'do nothing, already taken care of
Else
ws.Visible = xlVeryHidden
End If
Next ws
End Sub

Remember that the workbook still has to be saved for this to work
effectively--if the user closes without saving, all your work is to naught.



Sean wrote:

I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub


--

Dave Peterson




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Issue on Close Q

The only change I would make to Dave's otherwise fine code is to change

If lcase(ws.Name) = lcase("Splash") Then
to
If StrComp(ws.Name, "Splash", vbTextCompare) = 0 Then

StrComp is a much more efficient way to compare strings than using the "="
operator, especially if you have to convert the two strings to upper or
lower case for the compare. It won't make a visible difference in the posted
code, but it is a good habit to get into.

A quick and dirty test using GetTickCount and looping 10 million times
showed StrComp is about 10 times faster than using "=" with LCase.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Sean" wrote in message
oups.com...
Thanks Guys, Fixed


Dave Peterson wrote:
And since one sheet must be visible, you might as make sure it's Splash:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet 'not worksheets
me.worksheets("splash").visible = xlsheetvisible
For Each ws In me.Worksheets
If lcase(ws.Name) = lcase("Splash") Then
'do nothing, already taken care of
Else
ws.Visible = xlVeryHidden
End If
Next ws
End Sub

Remember that the workbook still has to be saved for this to work
effectively--if the user closes without saving, all your work is to
naught.



Sean wrote:

I have the following code that, on close, it hides all the Sheets
except Splash. Problem is I'm getting a debug on -
ws.Visible = xlVeryHidden

It says - Unable to set the visible property of the worksheet class

I've all sheets protected and running 2003

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each ws In Worksheets
If ws.Name = "Splash" Then
ws.Visible = True
Else
ws.Visible = xlVeryHidden
End If
Next

End Sub


--

Dave Peterson




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I stop the close method in an auto close macro Paul Excel Programming 2 November 17th 06 02:48 PM
In Before Close Sub ActiveWorkBook.Close(False) repeat procedure [email protected] Excel Programming 5 September 26th 06 03:11 PM
Excel shoud not close all active books when clicking close button technomike Excel Discussion (Misc queries) 0 June 10th 05 05:35 PM
excel - Windows close button (x) should only close active workboo. CoffeeAdict Setting up and Configuration of Excel 3 February 8th 05 04:30 AM
Open / Close Workbook Memory issue David Edison Excel Programming 0 November 17th 04 10:09 AM


All times are GMT +1. The time now is 11:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"