• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 722
  • Last Modified:

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

0
Bob Berryman
Asked:
Bob Berryman
  • 6
  • 5
1 Solution
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now