PDA

Click to See Complete Forum and Search --> : [RESOLVED] BeforeClose


mikeymay
Apr 5th, 2006, 04:56 AM
I have the following routines inThisWorkbook but althougg I can get the auto_open to work I can't get the BeforeClose to work.

Anyone have any ideas?

Option Explicit

Private Sub auto_open()

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = "Error" Then
ws.Visible = xlSheetVisible
Else
End If
Next ws

Worksheets("Error").Visible = xlSheetVeryHidden

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.EnableEvents = False

MsgBox ("Closing")

If Not ThisWorkbook.Saved Then
Else
Worksheets("Error").Visible = xlSheetVisible
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = "Error" Then
ws.Visible = xlSheetVeryHidden
Else
ws.Visible = xlSheetVisible
End If
Next ws
ThisWorkbook.Save
End If

End Sub


Thanks

si_the_geek
Apr 5th, 2006, 05:42 AM
Moved to Office Development forum.

zaza
Apr 5th, 2006, 06:15 AM
Shall I go through your code and first decipher what it supposed to do before trying to figure out how it isn't working? Or can you answer those questions and make it a lot easier?

zaza

mikeymay
Apr 5th, 2006, 06:21 AM
Actually I think I have just cracked it.

Need to restart Excel when after the code has been dropped in. Not sure why, but it seems to work.

RobDog888
Apr 5th, 2006, 07:19 AM
Why would you need to restart Excel to make this work?

What version of Excel are you running? You shouldnt use the Auto_Open event as its old and only for backwards compatibility.

mikeymay
Apr 5th, 2006, 07:54 AM
Don't know.

Just seemed to work......

What should I use instead of auto_open?

RobDog888
Apr 5th, 2006, 07:55 AM
Private Sub Workbook_Open()
MsgBox "Open"
End Sub:)

mikeymay
Apr 5th, 2006, 08:02 AM
In the Auto_Open (nowWorksheet_Open) I want to place a call to another routine that is in a module.

It doesn't seem to recognise the sub name. How do I call another routine when script is behind ThisWorkbook?


Thanks

RobDog888
Apr 5th, 2006, 08:10 AM
The sub in the module should be declared as Friend or Public.
Public Sub SomeSubOfMine()
MsgBox "Meow!"
End SubThen call as usual from ThisWorkbook.
Private Sub WorkBook_Open()
SomeSubOfMine
End Sub

mikeymay
Apr 5th, 2006, 08:16 AM
Right, I think I have it tied down now. Thanks all for the help.

RobDog888
Apr 5th, 2006, 08:19 AM
Np, dont forget to Resolve the thread then. ;)