auto save on close, duplicating file

JasonH001

New Member
Joined
Feb 12, 2021
Messages
6
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
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

JasonH001

New Member
Joined
Feb 12, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
This works. It prompts the read-only user and asks if they want to save or not.

I appreciate it. Thank you!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,145
Members
415,881
Latest member
tasic89

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
Top