+ Reply to Thread
Results 1 to 6 of 6

Macro to rename files

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

    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
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,331

    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, click the 'Go Advanced' button, Then click 'Manage Attachements' and follow the menu.
    Thinking I am smart and knowing I am smart are two entirely different levels of schizophrenia.

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

    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
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,331

    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
    5

    Re: Macro to rename files

    Thanks JLGWhiz!

  6. #6
    Forum Expert
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    1,331

    Re: Macro to rename files

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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