Update macro to just auto overwrite if file name already exists

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,
This macro works great, but if there is already a file with the same name it dugs out,
i want it to just overwrite the file.
can this be done?
if so, lease update the attached.
thanks
Tony

my code
VBA Code:
Sub Create_Report()
 Call AA_Protect
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False


myLoc = Application.ActiveWorkbook.Path
NewName = Range("O10")

ActiveSheet.Copy

ActiveWorkbook.SaveAs myLoc & "\" & NewName & ".xlsx"

MsgBox "Report Created"


Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
 Call AA_unProtect
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Turning off alerts with this line of code:
VBA Code:
Application.DisplayAlerts = False
should suppress those warnings.

Are you sure that isn't a different issue, such as the file with the same name that you want to overwrite is already open on your computer or someone else's (you cannot overwrite an open file)?
Or that you have an invalid file name in cell O10 (what value exactly is in this cell)?

What is the exact message you are getting?
 
Upvote 0
If it still does not work, please provide us with the value in cell O10 and let us know the exact message you are getting.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

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