+ Reply to Thread
Results 1 to 7 of 7

Macro to rename files

  1. #1
    Registered User
    Join Date
    05-08-2019
    Location
    Florida
    MS-Off Ver
    2016
    Posts
    6

    Macro to rename files

    Hello Everyone,

    I have a macro that renames files based on the value in cell B3, and I'm running into an issue with file names sometimes coming up with the same value. When it reaches a point where it ends up renaming a file the same as another file the macro gets an error. Does anybody know of a way to make the macro add a (1) at the end of a file name if another file already has that name it is attempting to rename it to? Or if that isn't possible, if it could just skip that file and rename all the rest that would work as well, since manually renaming one or two files wouldn't be too difficult.

    Edit: It appears when it runs into this error it also deletes one of the files that would've had a duplicate name, and then returns the error.

    Please Login or Register  to view this content.
    Last edited by Jhoov326; 05-15-2019 at 02:15 PM.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to rename files

    This modified version of your code will allow files that error to be skipped but will produce a message box after the entire directory has processed that shows which files, if any, were skipped. If none were skipped no message will appear.
    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    05-08-2019
    Location
    Florida
    MS-Off Ver
    2016
    Posts
    6

    Re: Macro to rename files

    Quote Originally Posted by JLGWhiz View Post
    This modified version of your code will allow files that error to be skipped but will produce a message box after the entire directory has processed that shows which files, if any, were skipped. If none were skipped no message will appear.
    Please Login or Register  to view this content.
    Thanks for the help JLGWhiz. I'm getting an error with this code unfortunately "Compile error: Invalid qualifier" The debugger is highlighting "strWF" in the line "skpFile = skpFile & vblf & strWF.Name"

    Any ideas?

    Thanks!

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to rename files

    My error. Remove the '.Name' from that line of code.
    The strWF is the Name so it should show up in the message OK. I was mistakenly treating strWF as workbook object instead of a string file name.

  5. #5
    Registered User
    Join Date
    05-08-2019
    Location
    Florida
    MS-Off Ver
    2016
    Posts
    6

    Re: Macro to rename files

    Thanks JLGWhiz!

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to rename files

    Quote Originally Posted by Jhoov326 View Post
    Thanks JLGWhiz!
    You're welcome,
    Regards, JLG

  7. #7
    Registered User
    Join Date
    05-08-2019
    Location
    romania
    MS-Off Ver
    2010
    Posts
    3

    Re: Macro to rename files

    Hi, can someone help? I need this code to take into consideration also the situation when I have duplicate excels. If all files in the directory are renamed after cell b2 inside each excel and if one file was already renamed with that cell content, then if another file wants to get renamed the same, then the 2nd file should be renamed with _v2 at the end .

    Customer. xls
    Customer_v2.xls
    Last edited by rbl89; 07-13-2020 at 10:23 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. Rename multiple files in a folder with a macro - Existing Macro update
    By nobleprince in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-11-2016, 05:39 PM
  2. My File Rename Macro Always Doesn't Rename the Last Three files in the list
    By Enright in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2016, 03:46 PM
  3. [SOLVED] Macro to list files then Rename them
    By boss1982 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-26-2014, 12:38 PM
  4. macro to rename pdf files
    By somusas in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-25-2013, 03:13 AM
  5. Rename files with a macro
    By bensalisbury in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2012, 07:42 AM
  6. Rename files with excel macro.
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-06-2010, 11:49 AM
  7. Macro to rename and move files
    By Caroline25 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2007, 03:11 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