VBA code to rename the files in a folder as per respective file cell values.

amaresh achar

Board Regular
Joined
Dec 9, 2016
Messages
108
Office Version
  1. 365
Platform
  1. Windows
I am looking for VBA code assistance to do the following:

>> I have about 3000 files in a folder (with some random alphanumeric values as file names)
>> Each file has unique values in cells B3, D7, G5 of Sheet1
>> I want a macro to rename each file with respective cell values as "B3_D7_G5"

Thank you so much in advance...!

This post is very much similar to following older post:


requirement.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try the below


VBA Code:
Function GetFilesIn(folder As String) As Collection
  Dim f As String
  Set GetFilesIn = New Collection
  f = Dir(folder & "\*.xlsx")
  Do While f <> ""
    GetFilesIn.Add f
    f = Dir
  Loop
End Function

Function GetFoldersIn(folder As String) As Collection
  Dim f As String
  Set GetFoldersIn = New Collection
  f = Dir(folder & "\*", vbDirectory)
  Do While f <> ""
    If GetAttr(folder & "\" & f) And vbDirectory Then GetFoldersIn.Add f
    f = Dir
  Loop
End Function


Sub Test_rename_files_based_on_cell_Contents()
  Dim Cfiles As Collection, Ffiles
  Dim Cfolder As Collection, Ffolder
  Dim path As String, newpath As String
  Dim newprefix As String, x As Integer
  Dim fext As String, newfilename As String
  Dim wb As Workbook
 

  path = "C:\xxx\yyy\Folders\testfolder\"
 

  'Debug.Print "Folders in " & path
  Set Cfolder = GetFoldersIn(path)
  For Each Ffolder In Cfolder
  x = 0 ' reset file counter
    'Debug.Print Ffolder
    newpath = path & Ffolder
    'Debug.Print newpath
    Set Cfiles = GetFilesIn(newpath)
    'Debug.Print Cfiles.Count
   
   
    If Cfiles.Count > 0 Then
   
    For Each Ffiles In Cfiles
    'x = x + 1
   
    Set wb = Workbooks.Open(newpath & "\" & Ffiles)
    If wb.Sheets.Count >= 1 Then
        newfilename = wb.Sheets("Sheet1").Range("B3").Value & "_" & wb.Sheets("Sheet1").Range("D7").Value & "_" & wb.Sheets("Sheet1").Range("G5").Value
    Else
        newfilename = ""
    End If
    wb.Close False
   
   
      'Debug.Print Ffiles
     
      fext = Mid(Ffiles, InStr(1, Ffiles, "."), 5)
      Name newpath & "\" & Ffiles As newpath & "\" & newfilename & fext
     
    Next Ffiles
    End If
   
  Next Ffolder
 
End Sub
 
Upvote 0
Solution
Try the below


VBA Code:
Function GetFilesIn(folder As String) As Collection
  Dim f As String
  Set GetFilesIn = New Collection
  f = Dir(folder & "\*.xlsx")
  Do While f <> ""
    GetFilesIn.Add f
    f = Dir
  Loop
End Function

Function GetFoldersIn(folder As String) As Collection
  Dim f As String
  Set GetFoldersIn = New Collection
  f = Dir(folder & "\*", vbDirectory)
  Do While f <> ""
    If GetAttr(folder & "\" & f) And vbDirectory Then GetFoldersIn.Add f
    f = Dir
  Loop
End Function


Sub Test_rename_files_based_on_cell_Contents()
  Dim Cfiles As Collection, Ffiles
  Dim Cfolder As Collection, Ffolder
  Dim path As String, newpath As String
  Dim newprefix As String, x As Integer
  Dim fext As String, newfilename As String
  Dim wb As Workbook
 

  path = "C:\xxx\yyy\Folders\testfolder\"
 

  'Debug.Print "Folders in " & path
  Set Cfolder = GetFoldersIn(path)
  For Each Ffolder In Cfolder
  x = 0 ' reset file counter
    'Debug.Print Ffolder
    newpath = path & Ffolder
    'Debug.Print newpath
    Set Cfiles = GetFilesIn(newpath)
    'Debug.Print Cfiles.Count
  
  
    If Cfiles.Count > 0 Then
  
    For Each Ffiles In Cfiles
    'x = x + 1
  
    Set wb = Workbooks.Open(newpath & "\" & Ffiles)
    If wb.Sheets.Count >= 1 Then
        newfilename = wb.Sheets("Sheet1").Range("B3").Value & "_" & wb.Sheets("Sheet1").Range("D7").Value & "_" & wb.Sheets("Sheet1").Range("G5").Value
    Else
        newfilename = ""
    End If
    wb.Close False
  
  
      'Debug.Print Ffiles
    
      fext = Mid(Ffiles, InStr(1, Ffiles, "."), 5)
      Name newpath & "\" & Ffiles As newpath & "\" & newfilename & fext
    
    Next Ffiles
    End If
  
  Next Ffolder
 
End Sub
Hi, I_know_nuffin... The code is working as I expected... but it opens a random excel file from the path in which this macro file is placed. how to avoid that..??

Thank you so much for the help...
 
Upvote 0
it should only open 'xlsx' files in the below Folder Path or its subfolders

path = "C:\xxx\yyy\Folders\testfolder\"


tested on my folders and it only opens the files in the test folders
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,246
Members
449,093
Latest member
Vincent Khandagale

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