Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Run code conditional on sheet visibility

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Run code conditional on sheet visibility

Try this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim runflag As Boolean
runflag = False
For x = 1 To Worksheets.Count
If Worksheets(x).Visible Then
Count = Count + 1
If Worksheets(x).Name = "LogGraph3" Then runflag = True
End If
Next
If Not runflag Then
Exit Sub
Else
If runflag And Count < 2 Then
Exit Sub
End If
End If
'some code
End Sub

Mike

"Sandy" wrote:

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Run code conditional on sheet visibility

Hi Mike

Something going on I don't understand here - if I use the code you supplied
I get the Count as 19 however after a bit of adjustment the following
produces the correct result i = 1. I stepped it through immediately after
your code in the Sub. Thing is only the LogGraph3 sheet was showing in the
application when I did the check.

Dim ws As Worksheet
Dim i As Long

i = 0
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = True Then
i = i + 1
End If
Next

If Sheets("LogGraph3").Visible = True And i = 1 Then

etc etc

Sandy

"Mike H" wrote in message
...
Try this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim runflag As Boolean
runflag = False
For x = 1 To Worksheets.Count
If Worksheets(x).Visible Then
Count = Count + 1
If Worksheets(x).Name = "LogGraph3" Then runflag = True
End If
Next
If Not runflag Then
Exit Sub
Else
If runflag And Count < 2 Then
Exit Sub
End If
End If
'some code
End Sub

Mike

"Sandy" wrote:

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Run code conditional on sheet visibility

Dim ws As Worksheet
Dim wsCount As Long
wsCount = 0
For Each ws In Worksheets

If ws.Visible = xlSheetVisible Then
wsCount = wsCount + 1
End If
Next

If wsCount = 1 Then
If Worksheets("Sheet2").Visible = True Then
MsgBox "One sheet visible and the were looking for"
Else
MsgBox "One sheet visible but not the were looking for"
End If
Else
MsgBox "More then one sheet visable"
End If

"Sandy" wrote:

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Run code conditional on sheet visibility

Hi Mike

Works perfectly!

Thank You

"Mike" wrote in message
...
Dim ws As Worksheet
Dim wsCount As Long
wsCount = 0
For Each ws In Worksheets

If ws.Visible = xlSheetVisible Then
wsCount = wsCount + 1
End If
Next

If wsCount = 1 Then
If Worksheets("Sheet2").Visible = True Then
MsgBox "One sheet visible and the were looking for"
Else
MsgBox "One sheet visible but not the were looking for"
End If
Else
MsgBox "More then one sheet visable"
End If

"Sandy" wrote:

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Run code conditional on sheet visibility

No Problem

"Sandy" wrote:

Hi Mike

Works perfectly!

Thank You

"Mike" wrote in message
...
Dim ws As Worksheet
Dim wsCount As Long
wsCount = 0
For Each ws In Worksheets

If ws.Visible = xlSheetVisible Then
wsCount = wsCount + 1
End If
Next

If wsCount = 1 Then
If Worksheets("Sheet2").Visible = True Then
MsgBox "One sheet visible and the were looking for"
Else
MsgBox "One sheet visible but not the were looking for"
End If
Else
MsgBox "More then one sheet visable"
End If

"Sandy" wrote:

Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate
that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Run code conditional on sheet visibility

Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Run code conditional on sheet visibility

Hi Rick

When I run your code I get X= 28 which is fine but the Count = -35. ??

Sandy

"Rick Rothstein" wrote in message
...
Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Run code conditional on sheet visibility

I don't see how that is possible as True in VBA evaluates to -1, so the
minus a minus number should result in adding, not subtracting. Just so you
know, I tested the code before I posted it and Count always came out
positive. In any event, whatever is causing the negative values on your
system should be able to be handled by this modification to what I posted
earlier...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count + Abs(Sheets(X).Visible)
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi Rick

When I run your code I get X= 28 which is fine but the Count = -35. ??

Sandy

"Rick Rothstein" wrote in message
...
Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet
visible then I do not require the Workbook_BeforeClose to run, how do I
incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 270
Default Run code conditional on sheet visibility

Hi Rick,

Something strange is happening here.

I copied your code EXACTLY, and placed it at the top of the
Workbook_BeforeClose. The final numbers for x and Count are now 28 and 37
respectively.

1st - how can the count of visible sheets be greater than the actual number
of sheets (27)?

2nd - while watching the x and Count add up in the Locals window I notice
that for the first 8 sheets Count remains at 0 - from x=9 to 22 Count
increases 2 at a time giving a total Count at this stage of 28. When x
changes to 23 the Count then increases to 29 (increments by 1) from this
point to x=27 the Count rises to 37(incrementing by 2 again). When x=28 the
code moves on from the 'For Next' i.e. x and Count complete.

3rd - only eight of my sheets are visible to the user - the rest are hidden
unless a particular sub (Sub Name()) is run which only leaves the, normally
hidden, "LogGraph3" sheet visible prior to closing. Thus if "LogGraph3"
sheet is the only visible sheet at this stage in the close down there is no
need to run Workbook_BeforeClose, because pretty much all it does is leave
"LogGraph3 sheet visible with a message to enable macros at start up. Which
is back to why I was looking for a test for open or visible sheets prior to
Workbook_BeforeClose running.

I hope the above makes sense.

Sandy

"Rick Rothstein" wrote in message
...
I don't see how that is possible as True in VBA evaluates to -1, so the
minus a minus number should result in adding, not subtracting. Just so you
know, I tested the code before I posted it and Count always came out
positive. In any event, whatever is causing the negative values on your
system should be able to be handled by this modification to what I posted
earlier...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count + Abs(Sheets(X).Visible)
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi Rick

When I run your code I get X= 28 which is fine but the Count = -35. ??

Sandy

"Rick Rothstein" wrote in message
...
Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet
visible then I do not require the Workbook_BeforeClose to run, how do I
incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Run code conditional on sheet visibility

I just set up a workbook with Rick's code and it ran properly. It looks thru
all sheets and IF visible counts them. If there is only one and the desired
sheet is that one it will do this. If not it will do that. That can be blank
if desired.
If all else fails, you may send me your workbook, to my address below, along
with instructions and I will look.
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Sandy" wrote in message
...
Hi Rick,

Something strange is happening here.

I copied your code EXACTLY, and placed it at the top of the
Workbook_BeforeClose. The final numbers for x and Count are now 28 and 37
respectively.

1st - how can the count of visible sheets be greater than the actual
number of sheets (27)?

2nd - while watching the x and Count add up in the Locals window I notice
that for the first 8 sheets Count remains at 0 - from x=9 to 22 Count
increases 2 at a time giving a total Count at this stage of 28. When x
changes to 23 the Count then increases to 29 (increments by 1) from this
point to x=27 the Count rises to 37(incrementing by 2 again). When x=28
the code moves on from the 'For Next' i.e. x and Count complete.

3rd - only eight of my sheets are visible to the user - the rest are
hidden unless a particular sub (Sub Name()) is run which only leaves the,
normally hidden, "LogGraph3" sheet visible prior to closing. Thus if
"LogGraph3" sheet is the only visible sheet at this stage in the close
down there is no need to run Workbook_BeforeClose, because pretty much all
it does is leave "LogGraph3 sheet visible with a message to enable macros
at start up. Which is back to why I was looking for a test for open or
visible sheets prior to Workbook_BeforeClose running.

I hope the above makes sense.

Sandy

"Rick Rothstein" wrote in message
...
I don't see how that is possible as True in VBA evaluates to -1, so the
minus a minus number should result in adding, not subtracting. Just so
you know, I tested the code before I posted it and Count always came out
positive. In any event, whatever is causing the negative values on your
system should be able to be handled by this modification to what I posted
earlier...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count + Abs(Sheets(X).Visible)
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi Rick

When I run your code I get X= 28 which is fine but the Count = -35. ??

Sandy

"Rick Rothstein" wrote in message
...
Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet
visible then I do not require the Workbook_BeforeClose to run, how do
I incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy



  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Run code conditional on sheet visibility

Can you post your code for the BeforeClose so we can see what is going on?

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi Rick,

Something strange is happening here.

I copied your code EXACTLY, and placed it at the top of the
Workbook_BeforeClose. The final numbers for x and Count are now 28 and 37
respectively.

1st - how can the count of visible sheets be greater than the actual
number of sheets (27)?

2nd - while watching the x and Count add up in the Locals window I notice
that for the first 8 sheets Count remains at 0 - from x=9 to 22 Count
increases 2 at a time giving a total Count at this stage of 28. When x
changes to 23 the Count then increases to 29 (increments by 1) from this
point to x=27 the Count rises to 37(incrementing by 2 again). When x=28
the code moves on from the 'For Next' i.e. x and Count complete.

3rd - only eight of my sheets are visible to the user - the rest are
hidden unless a particular sub (Sub Name()) is run which only leaves the,
normally hidden, "LogGraph3" sheet visible prior to closing. Thus if
"LogGraph3" sheet is the only visible sheet at this stage in the close
down there is no need to run Workbook_BeforeClose, because pretty much all
it does is leave "LogGraph3 sheet visible with a message to enable macros
at start up. Which is back to why I was looking for a test for open or
visible sheets prior to Workbook_BeforeClose running.

I hope the above makes sense.

Sandy

"Rick Rothstein" wrote in message
...
I don't see how that is possible as True in VBA evaluates to -1, so the
minus a minus number should result in adding, not subtracting. Just so
you know, I tested the code before I posted it and Count always came out
positive. In any event, whatever is causing the negative values on your
system should be able to be handled by this modification to what I posted
earlier...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count + Abs(Sheets(X).Visible)
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi Rick

When I run your code I get X= 28 which is fine but the Count = -35. ??

Sandy

"Rick Rothstein" wrote in message
...
Give this structure a try...

'******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
Dim Count As Long
For X = 1 To Sheets.Count
Count = Count - Sheets(X).Visible
Next
If Not Sheets("LogGraph3").Visible Or Count 1 Then
' Do Closing Code
Else
' Do Early End Code
End If
End Sub
'******Sub Ends*******

--
Rick (MVP - Excel)


"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet
visible then I do not require the Workbook_BeforeClose to run, how do
I incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Run code conditional on sheet visibility

I looked at the workbook and made corrections and suggestions before
returning to OP.
Best to have you open event call another sub instead of having in
incorporated. Easier to test.
Using with can do more with each worksheet, etc

Sub foreachws1() 'like workbookopen except more efficient.

On Error Resume Next ' to skip non existent
For i = 1 To 8
With Sheets("sheet" & i)
.Unprotect Password:="password"
.Activate
ActiveWindow.DisplayHeadings = False
.Visible = True
.Protect Password:="password"
'MsgBox .Name
End With
Next i
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Sandy" wrote in message
...
Hi
I have a situation whereby if sheet "LogGraph3" is the only sheet visible
then I do not require the Workbook_BeforeClose to run, how do I
incorporate that into the following.

******Sub Starts******
Sub Workbook_BeforeClose(Cancel As Boolean)

If Sheets("LogGraph3") 'is the only visible sheet' Then
GoTo EarlyEnd

Else
Do closing code
End If

EarlyEnd:
Some Code

End Sub
******Sub Ends*******

Thanks
Sandy


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
conditional visibility of check box Erol Excel Discussion (Misc queries) 5 May 12th 09 08:03 PM
help with code for conditional summary sheet englishdad Excel Discussion (Misc queries) 4 January 27th 09 02:11 PM
Ensure macros enabled - C. Pearson's sheet visibility method Horatio J. Bilge, Jr. Excel Programming 0 December 3rd 07 05:36 PM
Toggle sheet visibility John[_130_] Excel Programming 2 October 11th 07 02:04 AM
Excel 2002? Where to store code for all spreadsheet visibility? Colin Johnson Excel Programming 0 February 26th 04 03:17 PM


All times are GMT +1. The time now is 07:26 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"