My Auto-Save vba is saving to the wrong location and I don't know why

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
Office Version
  1. 365
Platform
  1. Windows
Here's file path my code:

VBA Code:
    ChDir "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\" & year(Date) & "\"

    ActiveWorkbook.SaveCopyAs FileName:="Pulte Master " & Format(Now, "yyyymmdd") & ".xlsm"

I want the archived file path to look like this: \\GLC-SERVER\Pulte\Z_Pulte Master Archive\2022\Pulte Master 20220415.xlsm

Unfortunately what I'm getting is this: \\GLC-SERVER\Pulte\Pulte Master 20220415.xlsm

Here's the whole macro:

VBA Code:
Sub SaveWorkbook()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim rootDirectory As String
    Dim folderToBeCreated As String
    Dim path As String
      
        rootDirectory = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"

        folderToBeCreated = Format(Now, "yyyy")

        path = rootDirectory & folderToBeCreated

        If Len(Dir(rootDirectory, vbDirectory)) <> 0 Then
            If Len(Dir(path, vbDirectory)) = 0 Then
                VBA.MkDir (path)

            Else
            End If
        Else
        End If
      
    ChDrive "Z"
      
    ChDir "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\" & year(Date) & "\"

    ActiveWorkbook.SaveCopyAs FileName:="Pulte Master " & Format(Now, "yyyymmdd") & ".xlsm"
     
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

What am I missing?
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try it like
VBA Code:
ActiveWorkbook.SaveCopyAs FileName:=path = & "\Pulte Master " & Format(Now, "yyyymmdd") & ".xlsm"
 
Upvote 0
Remove the = sign after path.

Also you would be better of changing the name of your variable. You should never use VBA keywords for variables or procedure names.
 
Upvote 0
Remove the = sign after path.

Also you would be better of changing the name of your variable. You should never use VBA keywords for variables or procedure names.

So, change all instances of path to something like zpath?
 
Upvote 0
That should be fine.
You can also get rid of this line as it doesn't do anything
VBA Code:
ChDir "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\" & year(Date) & "\"
 
Upvote 0
That should be fine.
You can also get rid of this line as it doesn't do anything
VBA Code:
ChDir "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\" & year(Date) & "\"

OK, suggested changes made. Archived files now save to correct file. However, the script was altering the file path on the active workbook which is strange. What I did to fix this is add code to set the Hyperlink Base before and after the save procedure. Works fine now, but I'm really not sure why. Here's the final code:

VBA Code:
Sub SaveWorkbook()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    ActiveWorkbook.BuiltinDocumentProperties("Hyperlink Base") = "\\GLC-SERVER\Pulte\"

    Dim rootDirectory As String
    Dim folderToBeCreated As String
    Dim zpath As String
      
        rootDirectory = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"

        folderToBeCreated = Format(Now, "yyyy")

        zpath = rootDirectory & folderToBeCreated

        If Len(Dir(rootDirectory, vbDirectory)) <> 0 Then
            If Len(Dir(zpath, vbDirectory)) = 0 Then
                VBA.MkDir (zpath)

            Else
            End If
        Else
        End If
      
    ChDrive "Z"
      
    ActiveWorkbook.SaveCopyAs FileName:=zpath & "\Pulte Master " & Format(Now, "yyyymmdd") & ".xlsm"
    
    ActiveWorkbook.BuiltinDocumentProperties("Hyperlink Base") = ""
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

Thanks!
 
Upvote 0
Solution
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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