Multiple Commands In BeforeClose Sub

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
I have my file set up so that it displays one sheet if its opened with macros disabled. I am trying to add another commend into the BeforeClose sub that saves the workbook if the file is not read only. If it is read only, it should just close without a prompt for "Do you want to save..." or "A file named xxx already exists...".

I think the problem is that when the workbook is closed a change is made (hiding/unhiding) so excel prompts with a Save box.

I am a novice to VBA, but I have tried to get it to work. Can someone help please. This is the code I have currently:


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Warning").Visible = True
Sheets("Report").Visible = xlVeryHidden

If ActiveWorkbook.ReadOnly Then
Application.DisplayAlerts = False
Else
ActiveWorkbook.Save
End If

End Sub

Instead of Application.DisplayAlerts = False, I have also used ThisWorkbook.Saved=True

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Thanks very much, but it is still not working.

Perhaps I should have given you more details. I have a drawing object with Application.Quit assigned to it. The user would use that to close the spreadsheet, but the File menu or the x button.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top