MS Excel 2007 ThisWorkbook.SaveAs Filename Macro help

I have 3 Workbooks.  One is a "Master" workbook.  On it I have a macro that runs when a user clicks Save.  It saves any changes and then also saves a "Copy" workbook in another location.  Below is the code I'm using, and it works just fine.

The third Workbook is a timesheet uses a VLOOKUP formula, which points to the "Copy" workbook.

Recently I have discovered that if a user has their timesheet workbook open at the same time they are updating the "Master" workbook, the Macro code is changing the network path from the Copy to the Master.  

Any ideas on what's causing this and how to fix it?

Thanks.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveWorkbook.ReadOnly = False Then
'Uncomment the following line only if you need it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    ThisWorkbook.SaveAs Filename:= _
        "\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
    
    ThisWorkbook.SaveAs Filename:= _
         "\\PCES_FILESERVER\Project Management\Master Project Log\PCES Master Project Log.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
              
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End If
    Cancel = True
End Sub

Open in new window

LVL 1
Bob BerrymanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zorvek (Kevin Jones)ConsultantCommented:
Use the SaveAsCopy method to create a copy of the current workbook without changing the connection to the originally open workbook. The copy saved is closed.

   ThisWorkbook.SaveCopyAs "C:\Full\Path\To\File.xls"

This will preserve any links to the workbook.

Kevin
0
zorvek (Kevin Jones)ConsultantCommented:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveWorkbook.ReadOnly = False Then
'Uncomment the following line only if you need it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    ThisWorkbook.SaveAs Filename:= _
        "\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
   
    ThisWorkbook.SaveCopyAs Filename:= _
         "\\PCES_FILESERVER\Project Management\Master Project Log\PCES Master Project Log.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
             
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End If
    Cancel = True
End Sub

Kevin
0
zorvek (Kevin Jones)ConsultantCommented:
Actually, if the COPY is the one open, then this should work:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveWorkbook.ReadOnly = False Then
'Uncomment the following line only if you need it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    ThisWorkbook.Save
   
    ThisWorkbook.SaveAs Filename:= _
         "\\PCES_FILESERVER\Project Management\Master Project Log\PCES Master Project Log.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
             
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End If
    Cancel = True
End Sub

Kevin
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

zorvek (Kevin Jones)ConsultantCommented:
OK, I'm a little confused. You are working from the master, you save the master and it makes a copy. The other worksheet points to the copy but changes to point to the master? How is that possible if the copy is only created with a SaveAs? Instead of "the Macro code is changing the network path from the Copy to the Master" do you mean "the Macro code is changing the network path from the Master to the Copy"?

Kevin
0
Bob BerrymanAuthor Commented:
@zorvek - The COPY file is never actually opened and viewed by any users.  It resides in a network location that all users have permission to read from.  The MASTER file is what is usually left open by the user that is updating it.  If that particular user has their timesheet workbook open at the same time they do an update and save to the master, when they switch back to their timesheet, the macro code is the same, but the VLOOKUP formula is what has changed the network path from the COPY to the MASTER.

I wasn't clear before, sorry.
0
zorvek (Kevin Jones)ConsultantCommented:
Then use the SaveCopyAs on the copy and not the master:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveWorkbook.ReadOnly = False Then
'Uncomment the following line only if you need it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Application.EnableEvents = False

    ThisWorkbook.SaveCopyAs Filename:= _
        "\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
   
    ThisWorkbook.SaveAs Filename:= _
         "\\PCES_FILESERVER\Project Management\Master Project Log\PCES Master Project Log.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, WriteResPassword:="pcesmpl"
             
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End If
    Cancel = True
End Sub

Kevin
0
Bob BerrymanAuthor Commented:
I tried that and got this error. screen
0
zorvek (Kevin Jones)ConsultantCommented:
The problem we now have (and I neglected to remember earlier) is that SaveCopyAs does not accept a password as a parameter. Try this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If ActiveWorkbook.ReadOnly = False Then
        'Uncomment the following line only if you need it.
        'On Error Resume Next
        Application.DisplayAlerts = False
        Application.EnableEvents = False
   
        ThisWorkbook.SaveCopyAs _
            Filename:="\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm"
           
        Workbooks.Open _
            Filename:="\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm"
       
        ActiveWorkbook.SaveAs _
            Filename:="\\PCES_FILESERVER\PCES Job File Server\Master Project Log COPY\PCES Master Project Log COPY.xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, WriteResPassword:="pcesmpl"
       
        ThisWorkbook.Save
                 
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    End If
    Cancel = True
End Sub

Kevin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob BerrymanAuthor Commented:
It looks like that worked.  But is there another line of code to include possibly that can tell it to close the COPY workbook when it's done?
0
Bob BerrymanAuthor Commented:
It is also actually prompting me for the password even though the code has it included.
0
Bob BerrymanAuthor Commented:
I got the ActiveWorkbook.Close figured out myself.  Just wondering why it's prompting me for the password.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.