+ Reply to Thread
Results 1 to 23 of 23

VBA macro to open folder and rename files numerically

  1. #1
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    VBA macro to open folder and rename files numerically

    Hi folks, I am trying to get a VBA macro which I will assign to a button which will do the following when the button is clicked:

    1. Open a window for me to browse and navigate and open a specific folder
    2. Rename all the files in the folder in numerical order starting from 1, 2, 3 and so on.

    As a note, the files in the folder will already be in alphabetical order however I want the macro to loop through the folder and rename the alphabetical system to a numerical system.

    Thanks!
    If you have received help from a user, please feel free to Rep that person.
    Also, do remember to click Thread Tools and mark your thread as solved, if you have achieved a solution to your thread.

  2. #2
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256

    Re: VBA macro to open folder and rename files numerically

    Not test but you can copy your file to new folder to keep your file from damage
    Or with this code you can copy folder and open workbook and save as numeric
    Please Login or Register  to view this content.
    Last edited by daboho; 11-11-2018 at 07:44 PM.

  3. #3
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Thanks daboho but this did not work for me. I was only able to get the Dialog folder picker to open. Also, and most importantly, in my folders there are no Excel files. I believe you think that I want to rename Excel files from your code, but I don't have Excel files. The folders have different files like .txt text files, bitmap, Word and video files. So it would be nice if your code can be written to accommodate and rename any file types as long as they are in alphabetical format. Thanks!

  4. #4
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256
    Quote Originally Posted by chin67326 View Post
    Thanks daboho but this did not work for me. I was only able to get the Dialog folder picker to open. Also, and most importantly, in my folders there are no Excel files. I believe you think that I want to rename Excel files from your code, but I don't have Excel files. The folders have different files like .txt text files, bitmap, Word and video files. So it would be nice if your code can be written to accommodate and rename any file types as long as they are in alphabetical format. Thanks!
    Ok try again
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Thanks again but I tried the code and nothing happened. The file names remained unchanged.

  6. #6
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256
    Quote Originally Posted by chin67326 View Post
    Thanks again but I tried the code and nothing happened. The file names remained unchanged.
    Try again if not work tomorrow will be test
    Using copyfile
    Please Login or Register  to view this content.
    "ThankyouFor Attention * And Your Help!!"

  7. #7
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Thanks. It did not work and I got the below error:

    "Run-time error 76:

    Path not found"

    Also when I ran the below line I got a "Compile error: Invalid qualifier" error

    If (fso1.exists(newname.Name)) = True Then fso1.deletefile (v.Name)

    If you are able to test it with an actual folder tomorrow then that would be appreciated. Maybe you can create a folder and add multiple files likle text files, music and videos etc.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA macro to open folder and rename files numerically

    Try this on a testfolder.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 11-11-2018 at 10:43 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA macro to open folder and rename files numerically

    Hello chin67326,

    Here is another method using the Shell...

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA macro to open folder and rename files numerically

    @Leith Ross
    Thanks a lot for your great solutions

    If the windows settings are set to hide the extensions of the file names, the code will not assign the extension in the correct approach
    So I suggest instead of using .Name in the line
    Please Login or Register  to view this content.
    To use .Path instead

    Thank you very much
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA macro to open folder and rename files numerically

    Hello Yassir,

    As usual my friend, you are correct. I discovered that sometime ago but never wrote anything to determine if the file extensions were hidden, since I show them on my system.

    However, this macro will enable file extensions to be shown.

    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA macro to open folder and rename files numerically

    Thanks a lot Mr. Leith
    The code that you have posted and it is supposed to enable file extensions doesn't work for me. I tried to change 1 to 0 and 0 to 1 and the same ..

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA macro to open folder and rename files numerically

    Hello Yassir,

    According to Microsoft this should work for any NT disk system. If you are accessing your files from a network or the web then it probably won't work and should throw an error. If you are receiving any errors then add a break point to the macro at RegWrite and step through the code. If it makes to that point then it should be enabling/disabling the file extensions.

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA macro to open folder and rename files numerically

    That's my bad. It is working but I had to refresh the explorer ..
    Thanks a lot for the gift my tutor

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA macro to open folder and rename files numerically

    Hello Yassir,

    You're welcome. Glad you got it sorted out.

  16. #16
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256

    Re: VBA macro to open folder and rename files numerically

    hello chin67326 work for me
    only small change search file.name by non numeric

    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Hi all this is the original poster here. I have not had success therefore this thread has not been solved for me. Thank you all for your input. I will list the outcome I had with each attempt:

    bakerman2's input:

    When I tried the code, I received the below error:

    bakerman2.png


    Leith's input (also used/considered Yasser's advice):

    When I tried the code, I received the below error:

    leith.png

    Yasser, how did you get the code to work for you? I also refreshed explorer but the same error was present.

    dabaho's input:

    This approach has been slightly successful in achieving what I am looking for. However, for file names that has a period "." as part of the file name (e.g. "C - renovation list.part1", "D - renovation list.part2", when the code is run, the file changes from a Word file to an unreadable file [i.e. "3.part1", "4.part2"] with a white icon that looks like a sheet of paper and right clicking and selecting Properties reveals that the file opens with "Windows Shell Common Dll". Can you please tweak the code and test it so the files are renamed, even though there may be a period sign in the file name, and at the same time keep the original file type? See the before and after results which I have achieved below when I tried your code:

    dabaho.png


    Thanks all!

  18. #18
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA macro to open folder and rename files numerically

    I renamed some files like your before image and this was my outcome.
    After.PNG
    So what exactly do want to be your outcome.

  19. #19
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Thanks bakerman2! I want the outcome just like you achieved but I am looking for a code that can accommodate when Windows has the "hide known file extensions" turned on. Therefore I will obtain the same result as per your above without the .pdf, .xlsm, .xlsx etc. displayed. Therefore, I will obtain a result which just has the file named as 1, 2, 3, 4, 5 and 6 as per your example. I need to have "hide known file extensions" turned on and I believe that is leading to the error I am getting when I try your code. Can you do a code tweak that can accommodate systems that have the "hide known file extensions" turned on? Thanks again.

  20. #20
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256

    Re: VBA macro to open folder and rename files numerically

    i has test mycode if hide exstension and turn of file exstension
    and this work For me
    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: VBA macro to open folder and rename files numerically

    Hi chin67326,

    Please try below code

    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: VBA macro to open folder and rename files numerically

    Quote Originally Posted by daboho View Post
    i has test mycode if hide exstension and turn of file exstension
    and this work For me
    Please Login or Register  to view this content.
    This worked for me too. You have solved what I have been trying to achieve. Thank you! You have been repped.
    Last edited by chin67326; 11-13-2018 at 06:52 AM.

  23. #23
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,256
    Quote Originally Posted by chin67326 View Post
    This worked for me too. You have solved what I have been trying to achieve. Thank you! You have been repped.
    Thank for feedback

+ 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. [SOLVED] Rename multiple files in a folder with a macro
    By nobleprince in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-24-2016, 09:55 PM
  3. Macro to move files from one folder to another but rename duplicates
    By mark_anthony in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2014, 06:56 AM
  4. Macro that goes to Folder, selects- not open- files specified
    By Melissa Camp in forum Microsoft Windows Help
    Replies: 3
    Last Post: 09-09-2014, 10:38 AM
  5. Macro to open consecutively .TIFF files in 1 folder and rename them?
    By tedikoleva in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-09-2014, 03:28 PM
  6. Macro to Open specific files in a Folder
    By lorena.tgarza in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-15-2012, 09:01 AM
  7. Macro to open up all files in certain folder
    By dpcp in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2012, 03:26 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