Rename all files in a folder based on a cell value

TheHack22

Board Regular
Joined
Feb 3, 2021
Messages
121
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi experts,

Can someone help me with a VBA Code that will loop through a predefined folder and rename all the files in a folder based on cell B2?
Will need to incorporate the CLEAN Function because of the spaces in cell B2.

I looked online for VBA scripts to do this task, but couldn't find one that serves my purpose. Any help would be greatly appreciated.

Imran
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
here's a basic renaming app. It gets Source & target from the sheet, then renames all files in the vTargDir
alter it to fit your sheet:

Code:
Public Sub RenameAllFiles()
Dim fso, oFolder, oFile, oRX, vTargDir, vFindWord, vReplaceWord, vNewNameFull, vName
Dim sTxt As String, sFile As String
Dim vNewName
Const kiNUM = 2
'find & replace are CASE SENSITIVE
 
vTargDir = FixDir(Range("B1").Value)
vFindWord = LCase(Range("B3").Value)
vReplaceWord = Range("B4").Value
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(vTargDir)
 
  'results of change
Range("A10:B200").Clear
Range("A10").Select
For Each oFile In oFolder.Files
  vName = LCase(oFile.Name)
  If InStr(vName, vFindWord) > 0 Then GoSub RenameIt
Next
Set oFile = Nothing
Set oFolder = Nothing
Set fso = Nothing
MsgBox "Done"
Exit Sub
RenameIt:
    vNewName = Replace(vName, vFindWord, vReplaceWord)
    vNewNameFull = vTargDir & vNewName
    Name oFile As vNewNameFull
    ActiveCell.Value = vNewName
    ActiveCell.Offset(1, 0).Select  'next row
Return
End Sub

Private Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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