Results 1 to 6 of 6

Thread: Same File Name, New Location
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member Muleskin57's Avatar
    Join Date
    Dec 2016
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Same File Name, New Location

    I've created a tool to manage resources for disabled persons in a group home setting. It will be used by non-excel users mostly. I'm trying to create a macro that saves the existing file to a backup directory before exiting. Anyone have experience with this? I'm hitting my frustration point.

  2. #2
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    49,670
    Post Thanks / Like
    Mentioned
    51 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Same File Name, New Location

    You can use the Workbook_BeforeClose event procedure, which is VBA code that automatically run when they try to close the workbook.
    We can capture the file name, and have it automatically save to some pre-determined backup location (hard-coded in the VBA code).

    Paste the VBA code below in the "ThisWorkbook" module in VBA (it HAS to be in that module to work automatically).
    Change the file path to wherever you want it saved.
    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        
        Dim buPath As String
        Dim wbName As String
    
    '   Enter backup file path (ending in a "\")
        buPath = "C:\Temp\"
    
    '   Capture just the name of the current file
        wbName = ActiveWorkbook.Name
        
    '   Save file to backup directory
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs buPath & wbName
        Application.DisplayAlerts = True
        
    End Sub
    Also note that they must have Macros/VBA enabled for this code (or any automated VBA code) to work.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    New Member Muleskin57's Avatar
    Join Date
    Dec 2016
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Same File Name, New Location

    That's great! If the file is to be saved on a subfolder of it's current location(subfolder named "backup"), how would I state that? Multiple locations would be using this form on a network location specific to each home. We have write only sub folders (backup) that we want to dump the file into after the month end macro runs.

  4. #4
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    49,670
    Post Thanks / Like
    Mentioned
    51 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Same File Name, New Location

    If the file is to be saved on a subfolder of it's current location(subfolder named "backup"), how would I state that?
    Just change this line:
    Code:
    buPath = "C:\Temp\"
    to
    Code:
    buPath = ActiveWorkbook.Path & "\backup\"
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  5. #5
    New Member Muleskin57's Avatar
    Join Date
    Dec 2016
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Same File Name, New Location

    Thanks! Works great. You're my hero.

  6. #6
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    49,670
    Post Thanks / Like
    Mentioned
    51 Post(s)
    Tagged
    11 Thread(s)

    Default Re: Same File Name, New Location

    You are welcome!
    Glad I was able to help.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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