VBA - Save As to File Location...but if Existing File is Open by Another User

Status
Not open for further replies.

johnny51981

Active Member
Joined
Jun 8, 2015
Messages
406
Howdy:
I have a set of Excel Processes that I run each day, they refresh through Power Query, and then I Save As specific sheets as CSV files. The VBA provided below is how it looks now, the "FilePath" named range is currently set to a folder on my local machine.

My question is:
I would like to Save As the CSV files directly to a Shared Network Drive, but sometimes users of said CSVs leave the previous update open from the Network location (rather than moving a copy to their computer). Is there a way to run the below process if it is free to save over, but if it is held up by another user, it would Save As with a different file name, such as including the current date to the end of the file name?

VBA Code:
Sub ExportUnbilledWIP()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim TargetFolder As String
    TargetFolder = Range("FilePath")

    ThisWorkbook.Sheets("Unbilled WIP with Daily Excel").Copy
    ActiveWorkbook.SaveAs Filename:=TargetFolder & "Unbilled WIP with Daily Excel - CUSTOMER.CSV", FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.Close

    MsgBox "Unbilled WIP with Daily Excel - CUSTOMER file export is now complete"
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Good to hear you got the solution.

If you would like to post the solution then it is perfectly fine to mark your post as the solution to help future readers. Otherwise, please do not mark a post that doesn't contain a solution.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,714
Messages
6,174,050
Members
452,542
Latest member
Bricklin

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