+ Reply to Thread
Results 1 to 20 of 20

Match file name to containing folder

Hybrid View

  1. #1
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Match file name to containing folder

    Is it possible to automatically rename a file to match the containing folder upon saving?

    We have a network folder that contains multiple client folders and in each folder are subfolders for different phases of work and in one of these folders is a macro-enabled Excel file we use for client estimates. Each priamry client folder is named in the format: Client #-Name (200956-Central Bank Florida). What I would like to happen is whenever a new estimate is created and subsequently saved by the user to have that file automatically renamed to match the primary folder (200956-Central Bank Florida) which contains it.

    Hopefully this makes sense. I've searched to no luck but I may not be using the correct terms in my search.

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Here is an approach. When the user attempts to close the file, the code reads the parent file directory and pulls out the name in parenthesis and then carries out a saveas with the suggested name as the one in parenthesis. The original file is unaffected.

    Place this code in the workbook module

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        saveparentname
    End Sub
    Place the following code in any module

    Sub saveparentname()
    Dim folder, name As String
    
    folder = ThisWorkbook.Path
    name = Mid(folder, WorksheetFunction.Find("(", folder), Len(folder) - WorksheetFunction.Find("(", folder) + 1)
    name = Application.GetSaveAsFilename(InitialFileName:=name, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
    ActiveWorkbook.SaveAs Filename:=name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub

  3. #3
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    I added the first code to VBAProject>MS Excel Objects>ThisWorkbook and the second code to VBAProject>MS Excel Objects>Sheet1 but am getting the following error- 1004 Unable to get the Find Property of the WorksheetFunction class.

    Debug shows the error happening on line: name = Mid(folder, WorksheetFunction.Find("(", folder), Len(folder) - WorksheetFunction.Find("(", folder) + 1)

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Update the second module to this

    Sub saveparentname()
    Dim folder, name As String
    folder = ThisWorkbook.Path
    name = Mid(folder, Application.WorksheetFunction.Find("(", folder), Len(folder) - Application.WorksheetFunction.Find("(", folder) + 1)
    name = Application.GetSaveAsFilename(InitialFileName:=name, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
    ActiveWorkbook.SaveAs Filename:=name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub
    I think this may not work on your version, but this update is worth a try

  5. #5
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    I re-wrote the module and removed dependence on Find function

    Sub saveparentname()
    Dim folder, name As String
    folder = ThisWorkbook.Path
    name = Mid(folder, InStr(folder, "("), Len(folder) - InStr(folder, "(") + 1)
    name = Application.GetSaveAsFilename(InitialFileName:=name, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
    ActiveWorkbook.SaveAs Filename:=name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub

  6. #6
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    I updated the second module per your last suggestion and am now getting the sub or function not defined error. I cut and paste everything and don't see any typos so don't know why it can't find it.

    I've attached screenshots of what I have if it helps

    I appreciate your help on this

    Attachment 718225Attachment 718227

  7. #7
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Did you use the code from post #5?

  8. #8
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    Yes I did use the code from Post 5.

    Just realized my images did not attach properly so I've added them here

    Workbook Module.png
    Module-1.png
    Error.png

  9. #9
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Try moving the saveparentname code into a standard code module, not the sheet module. VBAProject>Modules>Module*
    Last edited by maniacb; 02-12-2021 at 10:29 AM.

  10. #10
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    Moving into a standard module cleared the compile error however it hangs on line name = Mid(folder, InStr(folder, "("), Len(folder) - InStr(folder, "(") + 1) when running.

  11. #11
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    I ran into the same error if I open the file when it was not in a folder/directory with a client name. The process I am using is expecting that the file will be moved (through drag/drop in Win Explorer) to the desired directory, than opening the file from that client folder. When the file is closed, the code will read the file's path and read the client name from that path.

    The code will throw an error if the file is opened from a path without a client name. If this is how it is going to be used, I can add code for the user to select the path. Is that what you want to do?

  12. #12
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    Quote Originally Posted by maniacb View Post
    I ran into the same error if I open the file when it was not in a folder/directory with a client name. The process I am using is expecting that the file will be moved (through drag/drop in Win Explorer) to the desired directory, than opening the file from that client folder. When the file is closed, the code will read the file's path and read the client name from that path.....
    This is the method that we use for our folder structure which looks like this-G:\Shared drives\ML310\2021 ESTIMATES & PROJECTS\210514-21SP0014-Client North Gate\Worksheets-Proposal\Worksheets-Takeoffs. In the Worksheets-Takeoffs folder is where the excel spreadsheet is located.

  13. #13
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Here is code that will ask for the directory name at close.

    Sub saveparentname()
    Dim folder, name As String
    On Error Resume Next
    With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please select a folder"
            .AllowMultiSelect = False
            .Show
            folder = .SelectedItems(1)
    End With
    name = Mid(folder, InStr(folder, "("), Len(folder) - InStr(folder, "(") + 1)
    name = Application.GetSaveAsFilename(InitialFileName:=folder & "\" & name, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
    ActiveWorkbook.SaveAs FileName:=name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End Sub

  14. #14
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Here is a better solution that will not ask to save in directory if the file is already in a client folder.

    Sub saveparentname()
    Dim folder, name, fold, nam, namwb As String
    On Error Resume Next
    fold = ThisWorkbook.Path
    nam = Mid(fold, InStr(fold, "("), Len(fold) - InStr(fold, "(") + 1)
    namwb = Mid(ThisWorkbook.name, 1, Len(ThisWorkbook.name) - 5)
    If nam <> namwb Then
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please select a folder"
            .AllowMultiSelect = False
            .Show
            folder = .SelectedItems(1)
        End With
        name = Mid(folder, InStr(folder, "("), Len(folder) - InStr(folder, "(") + 1)
        name = Application.GetSaveAsFilename(InitialFileName:=folder & "\" & name, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
        ActiveWorkbook.SaveAs FileName:=name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End If
    End Sub

  15. #15
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    So there are no parentheses in the folder name? Will the word client always be in the folder structure? Please provide a second client folder structure so I can test the code.
    Last edited by maniacb; 02-15-2021 at 03:58 PM.

  16. #16
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    Correct no parentheses. The naming (210514-21SP0014-Client North Gate) changes with each new estimate.

    210514-21SP0014 estimate number which increases by 1 with each new estimate 210515, 210516, etc.
    Client North Gate also changes based upon the actual client/ project. so 210516 might be Bank of America Orlando and 210517 could be Dade County Airport so they will never be the same.

  17. #17
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    To confirm, in the above example, the file name should be:

    210514-21SP0014-Client North Gate

    Please confirm

  18. #18
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    Quote Originally Posted by maniacb View Post
    To confirm, in the above example, the file name should be:

    210514-21SP0014-Client North Gate

    Please confirm
    That is correct

  19. #19
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Match file name to containing folder

    Here is an updated module

    Sub saveparentname()
    Dim folder, name, fold, nam, namwb As String
    Dim client, slshAftCl, slshBefCL, client2, slshAftCl2, slshBefCL2 As Long
    On Error Resume Next
    fold = ThisWorkbook.Path
    client = InStr(fold, "Client")
    slshAftCl = InStr(client, fold, "/", 1)
    slshBefCL = InStrRev(fold, "/", client, 1)
    name = Mid(fold, slshBefCL + 1, slshAftCl - slshBefCL - 1)
    namwb = Mid(ThisWorkbook.name, 1, Len(ThisWorkbook.name) - 5)
    If name <> namwb Then
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please select a folder"
            .AllowMultiSelect = False
            .Show
            folder = .SelectedItems(1)
        End With
        client2 = InStr(folder, "Client")
        slshAftCl2 = InStr(client2, folder, "\", 1)
        slshBefCL2 = InStrRev(folder, "\", client2, 1)
        nam = Mid(folder, slshBefCL2 + 1, slshAftCl2 - slshBefCL2 - 1)
        nam = Application.GetSaveAsFilename(InitialFileName:=nam, FileFilter:="Macro Enabled Workbook (*.xlsm), *.xlsm", Title:="Save As File")
    ActiveWorkbook.SaveAs FileName:=nam, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End If
    End Sub
    Last edited by maniacb; 02-15-2021 at 06:56 PM.

  20. #20
    Registered User
    Join Date
    12-12-2012
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Match file name to containing folder

    I think this latest code will work for what I am looking for. I greatly appreciate all of your time and help on this.

+ 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] Find files in folder where part of file name match Range and attach to Mail.
    By ANDREAAS in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 06-15-2020, 04:51 AM
  2. [SOLVED] Trouble Updating a list of Files if a file no longer exists in the Folder/Sub Folder Dir.
    By jrtraylor in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-26-2019, 02:03 AM
  3. Create Folder & Check if Folder Exists if Not Create Folder & then Save File
    By Quivolt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-28-2017, 05:31 AM
  4. how to know match file in folder without open file
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-30-2015, 08:33 PM
  5. Replies: 2
    Last Post: 12-29-2015, 04:19 AM
  6. Replies: 6
    Last Post: 08-11-2006, 03:41 PM
  7. macro to move from file to file, folder to folder
    By davegb in forum Excel General
    Replies: 0
    Last Post: 01-21-2005, 04:06 PM

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