auto save on close, duplicating file

JasonH001

New Member
Joined
Feb 12, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am hoping someone can help me modify the following code. Currently I am using this to bypass the save prompt and auto save the workbook on close. The issue I'm running into is that if I have the file open and a co-worker opens the file in read only mode, it will save a duplicate copy of the file. Some info that may or may not be helpful: These files are on a network location. The file is created in a certain location (default save location as listed under excel save properties). Once the file has been filled/populated it gets moved to a different file (our way of digitally filing, by customer name). The duplicate file re-appears in the default location with the same name, so its easy to tell that there are two. The auto save works perfectly fine as long as someone doesn't open in read-only. Can it be updated to not save if the file is in read-only mode?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub

Thanks in advance
Jason
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You could try this (untested)
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Me.ReadOnly Then Exit Sub
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
This works. It prompts the read-only user and asks if they want to save or not.

I appreciate it. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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