+ Reply to Thread
Results 1 to 9 of 9

VBA Code to SaveAs file in one location, and backup to another location

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    VBA Code to SaveAs file in one location, and backup to another location

    Hello,

    I have a code where when user selects SaveAs, it will automatically direct the user to the intended path and intended filename populated in saveas dialog box.

    How do I save a copy to another location using the same function? So it will have 2 copies living in 2 separate directories?

    I am assuming i have to run SaveAs and Save simultaneously but cant figure this out to save my life so some help would be greatly appreciated.


    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim vFilename As Variant
    Dim vPath As Variant
    
    'Disable events so "new save" doesn't re-trigger this event
    Application.EnableEvents = False
    
    If SaveAsUI = True Then ' User selected SaveAs instead of Save
    
        Cancel = True ' Cancel the user's original save action
        
            'This will make this shared before saving.
            If Not ActiveWorkbook.MultiUserEditing Then
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs ActiveWorkbook.Name, accessmode:=xlShared
            Application.DisplayAlerts = True
          
        End If
        
        Application.DisplayAlerts = False
        
        vPath1 = "\\tarcds01\eCTD_Submission\TRACKERS\" & Sheet1.Range("Q1").Value & "\" & Sheet1.Range("R1").Value & "\"
        vPath2 = Sheet1.Range("Q1").Value & "-" & Sheet1.Range("R1").Value & "-" & Sheet1.Range("S1").Value
     
        'Simulate built in "SaveAs", Use desired "IntialFilename" & filter
        vFilename = Application.GetSaveAsFilename(InitialFileName:=vPath1 & vPath2, fileFilter:="Regeneron Excel Macro-Enabled Worksheet (*.xlsm), *.xlsm")
        
        If vFilename <> False Then
    
            'Save file with desired parameters
            ThisWorkbook.SaveAs Filename:=vFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'Use desired FileFormat
            
        End If
        
        Debug.Print vFilename
        
        Application.DisplayAlerts = True
        
    End If

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA Code to SaveAs file in one location, and backup to another location

    Will the workbook name change?

    either way you can save AS to the backup location THEN save as to the location they choose for you.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA Code to SaveAs file in one location, and backup to another location

    ActiveWorkbook.SaveCopyAs Filename:= filePath & ActiveWorkbook.Name assuming filePath is the file path you want to use.

  4. #4
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code to SaveAs file in one location, and backup to another location

    Thanks for ur reply. The filename stays the same....so where would i put the code you suggested? As another private sub function? Or does it go in current private sub.

  5. #5
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA Code to SaveAs file in one location, and backup to another location

    So there are a few constraints apparently. It can only be saved as an xlsx file so this won't work for you BUT try the following shown in blue.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim vFilename As Variant
    Dim vPath As Variant
    Dim vBackupPath as string
    
    'Declare filepath
    vBackupPath = "C:\BACKUP\ 'this has to have the "\" at the end
    'You could also use a cell in the spreadsheet to house this if that is easier to audit
    
    'Disable events so "new save" doesn't re-trigger this event
    Application.EnableEvents = False
    
    If SaveAsUI = True Then ' User selected SaveAs instead of Save
    
        Cancel = True ' Cancel the user's original save action
        
            'This will make this shared before saving.
            If Not ActiveWorkbook.MultiUserEditing Then
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs ActiveWorkbook.Name, accessmode:=xlShared
            Application.DisplayAlerts = True
          
        End If
        
        Application.DisplayAlerts = False
        
        vPath1 = "\\tarcds01\eCTD_Submission\TRACKERS\" & Sheet1.Range("Q1").Value & "\" & Sheet1.Range("R1").Value & "\"
        vPath2 = Sheet1.Range("Q1").Value & "-" & Sheet1.Range("R1").Value & "-" & Sheet1.Range("S1").Value
     
        'Simulate built in "SaveAs", Use desired "IntialFilename" & filter
        vFilename = Application.GetSaveAsFilename(InitialFileName:=vPath1 & vPath2, fileFilter:="Regeneron Excel Macro-Enabled Worksheet (*.xlsm), *.xlsm")
        
        If vFilename <> False Then
    
            'Save Backup first
             ThisWorkbook.SaveAs Filename:= vBackupPath & ActiveWorkbook.Name 'I suppose you could use vFilename also
    
            'Save file with desired parameters
            ThisWorkbook.SaveAs Filename:=vFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'Use desired FileFormat
            
        End If
        
        Debug.Print vFilename
        
        Application.DisplayAlerts = True
        
    End If

  6. #6
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code to SaveAs file in one location, and backup to another location

    Great! Thanks for ur help. I will try this as soon as i get home in a few

  7. #7
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA Code to SaveAs file in one location, and backup to another location

    Sure, let me know because I just wrote it out and didnt test it myself or anything

  8. #8
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code to SaveAs file in one location, and backup to another location

    Thank you so much Mike, you guys are great.

    It wasnt working as intended at first because the SaveAs box came up twice, but i wiggled the code a bit to say SaveCopyAs and its working as intended. I really appreciate your help leading me on right path!

    ActiveWorkbook.SaveCopyAs Filename:=vBackupPath & vpath2 & ".xlsm"

  9. #9
    Registered User
    Join Date
    12-07-2013
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    91

    Re: VBA Code to SaveAs file in one location, and backup to another location

    well, i spoke too soon. This indeed works, it works because the first time, when user selects SaveAs it will save and make a copy. However, when the user opens the file again and user would select 'save', it will save the original file but it will not save over the copy in back-up folder.....what am i missing? Do i need to trigger something for Save?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim vFilename As Variant
    Dim vPath As Variant
    Dim vBackupPath As String
    
    'Disable events so "new save" doesn't re-trigger this event
    Application.EnableEvents = False
    
    If SaveAsUI = True Then ' User selected SaveAs instead of Save
    
        Cancel = True ' Cancel the user's original save action
        
            'This will make this shared before saving.
            If Not ActiveWorkbook.MultiUserEditing Then
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs ActiveWorkbook.Name, accessmode:=xlShared
            Application.DisplayAlerts = True
            'MsgBox "This Tracker is Shared"
        End If
        
        Application.DisplayAlerts = False
        
        vPath1 = "\\tarcds01\eCTD_Submission\TRACKERS\" & Sheet1.Range("Q1").Value & "\" & Sheet1.Range("R1").Value & "\"
        vpath2 = Sheet1.Range("Q1").Value & "-" & Sheet1.Range("R1").Value & "-" & Sheet1.Range("S1").Value
        vBackupPath = "\\tarcds01\eCTD_Submission\TRACKERS\backup-test\"
        'Simulate built in "SaveAs", Use desired "IntialFilename" & filter
        vFilename = Application.GetSaveAsFilename(InitialFileName:=vPath1 & vpath2, fileFilter:="Regeneron Excel Macro-Enabled Worksheet (*.xlsm), *.xlsm")
        
        If vFilename <> False Then
                         
             'Save Backup
            ThisWorkbook.SaveCopyAs Filename:=vBackupPath & vpath2 & ".xlsm"        
            'Save file with desired parameters
            ThisWorkbook.SaveAs Filename:=vFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            
                 
        End If
    
    
    
        Debug.Print vFilename
        
        Application.DisplayAlerts = True
        
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Specify export pdf file location is location workbook is saved.
    By dantray02 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2014, 01:13 PM
  2. Code that works in one location but not another location. (AdvancedFilter excel function)
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2013, 02:18 PM
  3. [SOLVED] [Help]macro to open file browser, to select a location, and save the location to a cell
    By zhuleijia in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-06-2013, 09:56 AM
  4. 2007 Excel auto backup file in different location
    By Shakeelqamar in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 01-01-2013, 07:16 PM
  5. Replies: 1
    Last Post: 10-31-2012, 11:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1