Loop through files in a folder and rename worksheet

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
55
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

Wondering if someone can help me with something. I have a list of csv files in a folder each containing a single tab. Is there a quick way to go into the file and rename the tab to Summary without opening each file and changing manually. Files are saved in \\users\TM\Desktop\Test\
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
There's really no "sheet name" in a CSV file. A CSV file is simply a text file containing comma separated values. And the worksheet that appears to store the values will always be named the same as the filename. So, for example, if you rename the worksheet, save, close, and reopen the file in Excel, you'll see that the worksheet will in fact show as the filename.

Hope this helps!
 
Upvote 0
Hi Domenic, thank makes sense to me. Just out of interest how would this work for a normal excel file?
 
Upvote 0
In order to rename a worksheet, the file needs to be opened. However, you can set screen updating to False, so that you won't see the workbooks opening and closing. So, for example, you would set screen updating to False at the beginning of your code, like this...

VBA Code:
Application.ScreenUpdating = False

Then you would set it back to True at the end of your code.
 
Upvote 0
The following code will loop through each file in the specified folder (change accordingly). For each file, it will open it, rename the first sheet as "Summary", Save, and then close the file. Note that if you want to refer to the worksheet by its name (assuming that all workbooks have the same name) instead of index number, replace...

VBA Code:
currentWorkbook.Worksheets(1).Name = "Summary"

with

VBA Code:
currentWorkbook.Worksheets("Sheet1").Name = "Summary"

Here's the code...

VBA Code:
Option Explicit

Sub RenameWorksheets()

    Application.ScreenUpdating = False
    
    Dim pathToFolder As String
    Dim currentFileName As String
    Dim fileCount As Long
    Dim currentWorkbook As Workbook
    
    pathToFolder = "c:\users\tom\desktop\new folder\" 'change the path accordingly
    If Right(pathToFolder, 1) <> "\" Then
        pathToFolder = pathToFolder & "\"
    End If
    
    currentFileName = Dir(pathToFolder & "*.xlsx", vbNormal)
    
    fileCount = 0
    Do While Len(currentFileName) > 0
        Set currentWorkbook = Application.Workbooks.Open(pathToFolder & currentFileName)
        currentWorkbook.Worksheets(1).Name = "Summary"
        currentWorkbook.Close SaveChanges:=True
        fileCount = fileCount + 1
        currentFileName = Dir
    Loop
    
    MsgBox "Number of files amended: " & fileCount
    
    Application.ScreenUpdating = True
    
End Sub

Hope this helps!
 
Upvote 0
Solution
The following code will loop through each file in the specified folder (change accordingly). For each file, it will open it, rename the first sheet as "Summary", Save, and then close the file. Note that if you want to refer to the worksheet by its name (assuming that all workbooks have the same name) instead of index number, replace...

VBA Code:
currentWorkbook.Worksheets(1).Name = "Summary"

with

VBA Code:
currentWorkbook.Worksheets("Sheet1").Name = "Summary"

Here's the code...

VBA Code:
Option Explicit

Sub RenameWorksheets()

    Application.ScreenUpdating = False
   
    Dim pathToFolder As String
    Dim currentFileName As String
    Dim fileCount As Long
    Dim currentWorkbook As Workbook
   
    pathToFolder = "c:\users\tom\desktop\new folder\" 'change the path accordingly
    If Right(pathToFolder, 1) <> "\" Then
        pathToFolder = pathToFolder & "\"
    End If
   
    currentFileName = Dir(pathToFolder & "*.xlsx", vbNormal)
   
    fileCount = 0
    Do While Len(currentFileName) > 0
        Set currentWorkbook = Application.Workbooks.Open(pathToFolder & currentFileName)
        currentWorkbook.Worksheets(1).Name = "Summary"
        currentWorkbook.Close SaveChanges:=True
        fileCount = fileCount + 1
        currentFileName = Dir
    Loop
   
    MsgBox "Number of files amended: " & fileCount
   
    Application.ScreenUpdating = True
   
End Sub

Hope this helps!
Hi Dom, This is really helpful thank you :)
 
Upvote 0
You're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

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