+ Reply to Thread
Results 1 to 22 of 22

Change all file names in folder based on cell values

  1. #1
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Smile Change all file names in folder based on cell values

    Hi people!.

    I am wondering, is it too difficult to make a macro that changes the name of all excel files in a folder based on the text contained in a given cell in each of those files.
    I download every month around 250+ excel files to a folder, all of which have random file names like "ls98jhy7jo976jjoyuf.xlsx". In each of those files, cell A1 contains the name of a client. I need to change in a batch all those files names to that of the client. Is this an appropiate job for a macro?is this a very advanced macro?. I have seen a few examples over the internet of similiar concepts, but i can?t get them to work exactly for this usecase (I am a total rookie here)

    Is RPA more appropiate for this? (I?m tinkering with UIpath)
    Last edited by bartolos; 08-09-2022 at 09:26 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    THIS VERSION MAKES A COPY AND RENAMES

    Please Login or Register  to view this content.




    THIS VERSION RENAMES EACH FILE

    Please Login or Register  to view this content.
    ASSUMPTIONS MADE ...
    1) Every file in folder is an excel workbook that you want changed
    2) The "new name" is in cell A1 of "Sheet1" in each workbook

    INSTRUCTIONS ...
    The only two lines you will need to change should be ...
    Const PATH_TO_FOLDER As String = "C:\Users\Nimrod\Documents\EXCEL\testDemo"
    Const SOURCE_SHEET_NAME = "Sheet1"
    Last edited by nimrod1313; 08-09-2022 at 04:22 PM.

  3. #3
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    Wow, did you write that on the fly?, it is fantastic!.
    I?ve made it work if I point the arrow and column to a cell that contains plain text, but the cell that contains the name of the client also contains characters that seem to cause an error, (I guess).
    The text in the target cell follows this pattern: eg: Empresa: 1057-TESLA S A NIF: B30072045

    Could these colons and dashes be causing trouble?

    Attachment 790987
    Last edited by bartolos; 08-09-2022 at 08:49 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    how about this ... I'm replacing anything thats not a character/number with "_" .

    Please Login or Register  to view this content.
    Last edited by nimrod1313; 08-09-2022 at 09:10 PM.

  5. #5
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    I receive an error message

    End Sub was expected

    mstsc_0UuKDLpxRg.png

    But if I rewrite it with an End Sub, I receive another error
    Last edited by bartolos; 08-09-2022 at 09:09 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    The following is working for me ... what's your error and what line does the error occur on ?


    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    Sorry, my bad. I forgot to change the sheet name in the last code you provided. IT WORKS!!!! "insert movie bells ringing here"

    Out of curiosity, If I started to learn VBA today, how long should it take me to be able to write code like this?

    (I followed a youtube tutorial last year, but now it is as if I suffered from amnesia)
    Last edited by bartolos; 08-09-2022 at 09:18 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    Anything else you'd like to tweak ??
    ... i'd appreicate it if you'd click and leave a comment .

  9. #9
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    Quote Originally Posted by nimrod1313 View Post
    Anything else you'd like to tweak ??
    ... i'd appreicate it if you'd click and leave a comment .
    Well, Nimrod, I feel bad for asking again, but, how would you add the current month at the beggining of the file name?.

  10. #10
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    Please Login or Register  to view this content.
    NOTE: If this has helped you please click on "add reputation" and leave a comment. thx

  11. #11
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    month and year

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    07-19-2022
    Location
    Hong Kong
    MS-Off Ver
    365
    Posts
    28

    Re: Change all file names in folder based on cell values

    Quote Originally Posted by bartolos View Post
    Well, Nimrod, I feel bad for asking again, but, how would you add the current month at the beggining of the file name?.
    You need to try many function if you can.

    Please Login or Register  to view this content.
    insert this line inside the loop, you know where to put?

  13. #13
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    It works like a charm, month and year first, then the file name, THANK YOU.

    As you said, Nimrod, it works in folders containing only excel files, otherwise, it breaks.

  14. #14
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    Here's a version that will allow you to look for xls* files in a folder with various files and only work on files that are excel

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    Cool. That?s right, an if then statement to run the following functions only when the file ends with xls.

    In case I wanted to put the value of another cell into a different variable newFileName0, so the expected file name was the combination of newFileName0 & "_" & newFileName

    would the following be correct? (I know it is not, I just wanted to show I?ve tried



    Public Sub ChangeFileNames()
    Dim oFSO As Object
    Dim newFileName As String
    Dim newFileName0 As String
    Dim fil

    Const PATH_TO_FOLDER As String = "C:\Users\glendon\Documents\EXCEL\testDemo"
    Const SOURCE_SHEET_NAME = "Sheet1"

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    For Each fil In oFSO.GetFolder(PATH_TO_FOLDER).Files
    If fil.Name Like "*.xls*" Then
    newFileName = GetInfoFromClosedBook(PATH_TO_FOLDER, fil.Name, SOURCE_SHEET_NAME, 1, 1) & "." & Split(fil.Path, ".")(1)
    newFileName = Replace(newFileName, ":", "_")
    newFileName = Replace(newFileName, " ", "_")
    newFileName = Replace(newFileName, "-", "_")
    newFileName = Format(Now(), "Mmm") & "_" & newFileName
    newFileName0 = GetInfoFromClosedBook(PATH_TO_FOLDER, fil.Name, SOURCE_SHEET_NAME, 5, 1) & "." & Split(fil.Path, ".")(1)
    newFileName0 = newFileName0 & "_" & newFileName


    oFSO.moveFile fil.Path, PATH_TO_FOLDER & newFileName0
    End If
    Next fil
    End Sub

    Public Function GetInfoFromClosedBook(Path, FileName, SheetName, RowNum, ColNum)
    GetInfoFromClosedBook = ExecuteExcel4Macro("'" & Path & "[" & FileName & "]" & SheetName & "'!R" & RowNum & "C" & ColNum)
    End Function

  16. #16
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    close ... but Your code will have the file extention added twice to the filename

    Try this ... (note untested)

    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    I've added a temporary msgbox that will let you see what your file name will be ... this should help you work out the kinks in your file naming tweaks

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    08-09-2022
    Location
    spain
    MS-Off Ver
    2016
    Posts
    8

    Re: Change all file names in folder based on cell values

    Hi Nimrod. Thanks for your help.
    This code throws an error for me after adding your modifications


    this is my code, please don?t scream in horror

    If you remember the text in the cell goes along these lines Empresa: 1057-TESLA S A NIF: B30072045
    I wanted to delete "numbers" and "NIF:" and replace "Empresa" with " - LISTADO COSTES -" so the resulting text would be something like "agosto 2022 - LISTADO COSTES - TESLA S A" this would work before the latest code addition to combine newFileName and newFileName2

    latest code, not working

    Please Login or Register  to view this content.

    Attachment 791140
    A rough translation would be "access route was not found"

    Attachment 791139

    The messagebox works fine, btw.
    Last edited by bartolos; 08-10-2022 at 10:56 PM.

  19. #19
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    Part of you "path" has a comma in it ... is that possible ??
    In short I think you're having a problem with you complex path and file name not being found or valid.
    Last edited by nimrod1313; 08-11-2022 at 04:15 AM.

  20. #20
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    To help you with your "folder path" I'm giving you two new versions/examples for you to examine

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    I've removed the requested info from my post ... please verify .

    I've also posted two new alternatives to setting up the source folder path.

    Cheers
    Nimrod

  22. #22
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Change all file names in folder based on cell values

    SUGGESTION: Follow the "DRY" rule of programming ... i.e. Don't Repeat Yourself
    one reason for DRY is eliminate typos ... like NIE X instead of NIF X

    REPLACE THIS ...
    Please Login or Register  to view this content.
    WITH THIS ...

    Please Login or Register  to view this content.
    AND THIS ...
    Please Login or Register  to view this content.
    Last edited by nimrod1313; 08-11-2022 at 10:43 AM.

+ 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. Replies: 0
    Last Post: 10-28-2021, 11:44 AM
  2. Create new folder and subfolder and save file with the names from cell values
    By eccordeiro in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-16-2015, 10:03 AM
  3. Requirement with folder names and file names
    By sateesh konduru in forum Excel General
    Replies: 3
    Last Post: 04-21-2015, 10:09 AM
  4. Import TXT files from a specific folder into Excel - TXT file names change hourly
    By jeepinjeff in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2013, 04:08 AM
  5. Macro to move file to another folder based on the names
    By kriahnadas.oo7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2013, 09:55 AM
  6. Seach in folder and copy data from file based on sheet names in master workbook
    By Hassų in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2012, 09:25 AM
  7. [SOLVED] Change file names in a folder
    By TISR in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2006, 08:40 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