ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel closing file problem (https://www.excelbanter.com/excel-programming/352106-excel-closing-file-problem.html)

Woody1313

Excel closing file problem
 
Hi

I have some code in the "Thisworkbook"

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim lastsheet As Long
lastsheet = Sheets.Count
For N = 2 To lastsheet
Sheets(N).Visible = False
Next
Me.Save
Me.Close

End Sub

This should hide , save and close the workbook once it has been closed.
Sometimes when the workbook is closed excel does not execute this code
and mearly just ask if you want to save and then just closes the
workbook without hiding the sheets.

Is there something I need to change in my code?

Thanks

John


Dave Peterson

Excel closing file problem
 
First, I'd remove the me.close line. That's gonna happen already.

Second, I couldn't break your code to do what you experienced.

Third, I've always found it better to put this kind of stuff in the
workbook_open event. That way, the workbook doesn't have to be saved by the
user.

If they open the workbook, really screw it up, your code will save it when they
close the workbook. Maybe not what you or the user wants.



Woody1313 wrote:

Hi

I have some code in the "Thisworkbook"

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim lastsheet As Long
lastsheet = Sheets.Count
For N = 2 To lastsheet
Sheets(N).Visible = False
Next
Me.Save
Me.Close

End Sub

This should hide , save and close the workbook once it has been closed.
Sometimes when the workbook is closed excel does not execute this code
and mearly just ask if you want to save and then just closes the
workbook without hiding the sheets.

Is there something I need to change in my code?

Thanks

John


--

Dave Peterson


All times are GMT +1. The time now is 12:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com