Using VBA to rename multiple of file names in folder but also taken parts of it out? Would this be possible.

Raboh001

New Member
Joined
May 10, 2022
Messages
8
Office Version
  1. 2021
Platform
  1. Windows
These file names are generally all the same but the numbers being completely different and I am just looking to place a ‘I’ at the start of the file name and removing the numbers at the start of it? The old name would be XS123445567_A_RT_Y 34019 and I would like to rename it too IXS123445567_A_RT_Y instead and I would have around 50 or these files names to change, if anyone has any ideas that would be amazing! Thank you again all
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Should be easy enough using Name function.
Instead of just using this line in that code
Name "C:\VBA Folder\Sample file 1.xlsx" As "C:\VBA Folder\Sample file 2.xlsx"
you would assign the old name to a variable and construct the new name by writing
strNewName = "1" & strOldName. However, it might be better to remove the trailing number first. The key though is consistency in the pattern of the strOldName - impossible to see a pattern with only one example, but it might be the space before the number.
 
Upvote 0
Hello,
I am just in need of help with renaming files very quickly. So I would have around 60 pdfs with names “EA12736458_A_PA_N 2103”, I would have to rename these files by placing a “I” at the start and removing the last 4 numbers at the end. The only difference each name has is the names are completely different like another file name would be “EA12364926_A_PA_N 2104”. The end result should look like this taking the first file name “IEA12736458_A_PA_N”. I use to get 100s of these files and just need to make the job more efficient. Thank you to anyone who can help!?
 
Upvote 0
Try this (just change the folder name to match your needs):
VBA Code:
Sub RenameFiles()

    Dim myDir As String
    Dim fn1 As String
    Dim fn2 As String

'   Set directory to look in
    myDir = "C:\Temp\Test\"
    
    fn1 = Dir(myDir & "*.pdf*")
    
'   Loop through files
    Do While fn1 <> ""
'       Check to see that file name does not begin with an "I"
        If Left(fn1, 1) <> "I" Then
'           Build new file name
            fn2 = "I" & Trim(Left(fn1, Len(fn1) - 8)) & ".pdf"
'           Rename files
            Name myDir & fn1 As myDir & fn2
'           Go to next file
        End If
        fn1 = Dir()
    Loop
    
    MsgBox "Renaming complete!"
    
End Sub
 
Upvote 0
I have merged your two threads together.

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted. If you have follow-up questions on a question, please post them back to the original thread instead of starting a new thread on the exact same question.
 
Upvote 0
Try this (just change the folder name to match your needs):
VBA Code:
Sub RenameFiles()

    Dim myDir As String
    Dim fn1 As String
    Dim fn2 As String

'   Set directory to look in
    myDir = "C:\Temp\Test\"
   
    fn1 = Dir(myDir & "*.pdf*")
   
'   Loop through files
    Do While fn1 <> ""
'       Check to see that file name does not begin with an "I"
        If Left(fn1, 1) <> "I" Then
'           Build new file name
            fn2 = "I" & Trim(Left(fn1, Len(fn1) - 8)) & ".pdf"
'           Rename files
            Name myDir & fn1 As myDir & fn2
'           Go to next file
        End If
        fn1 = Dir()
    Loop
   
    MsgBox "Renaming complete!"
   
End Sub
 
Upvote 0
Try this (just change the folder name to match your needs):
VBA Code:
Sub RenameFiles()

    Dim myDir As String
    Dim fn1 As String
    Dim fn2 As String

'   Set directory to look in
    myDir = "C:\Temp\Test\"
   
    fn1 = Dir(myDir & "*.pdf*")
   
'   Loop through files
    Do While fn1 <> ""
'       Check to see that file name does not begin with an "I"
        If Left(fn1, 1) <> "I" Then
'           Build new file name
            fn2 = "I" & Trim(Left(fn1, Len(fn1) - 8)) & ".pdf"
'           Rename files
            Name myDir & fn1 As myDir & fn2
'           Go to next file
        End If
        fn1 = Dir()
    Loop
   
    MsgBox "Renaming complete!"
   
End Sub
I’ve got a few lines that are red and then it won’t perform the task due to not having a macro name when pressing play. I will try and attach a photo but it’s saying it’s too large and just wondering how you also make a macro name to this task can be performed. Thank you for you help massively!
 
Upvote 0
I am not sure I understand what you are saying the issue is.
Where exactly have you placed this VBA code?
What is the name of the module you have placed it in?

You should be creating a new module, and placing it in there (do NOT put it in the "ThisWorkbook" or one of the "Sheet" modules).
If you place it in one of those other existing modules, you will not see it on your Macro listing from the worksheet.
 
Upvote 0
I’ve got a few lines that are red and then it won’t perform the task due to not having a macro name when pressing play. I will try and attach a photo but it’s saying it’s too large and just wondering how you also make a macro name to this task can be performed. Thank you for you help massively!
 

Attachments

  • 925B5019-FB33-48C1-B629-F7383258B3A6.jpeg
    925B5019-FB33-48C1-B629-F7383258B3A6.jpeg
    197 KB · Views: 47
Upvote 0
My
I am not sure I understand what you are saying the issue is.
Where exactly have you placed this VBA code?
What is the name of the module you have placed it in?

You should be creating a new module, and placing it in there (do NOT put it in the "ThisWorkbook" or one of the "Sheet" modules).
If you place it in one of those other existing modules, you will not see it on your Macro listing from the worksheet.
My apologies I haven’t started a new one so I will have to create a new one, but after this is what the code looks like. Not sure if I was suppose to put spaces or something between but it won’t run because of it.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top