VBA Code to Bulk Rename First Worksheet Dependent on Workbook Name

Breader11

New Member
Joined
Sep 23, 2012
Messages
6
I have a folder containing multiple Excel workbooks. I'd like to bulk rename the first worksheet (the typical Sheet1 position) in each workbook to match its workbook name. The worksheet names may or may not be named "Sheet1," so I don't want the code to assume anything, other than the position of the worksheet to be that first position. Any and all help would be greatly appreciated!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this - change the folder name to suit

Rich (BB code):
Sub RenSheets()
Dim MyFolder As String
Dim MyFile As String
Dim wbname As String
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Application.ScreenUpdating = False
Do While MyFile <> ""
    Workbooks.Open Filename:=MyFolder & "\" & MyFile
    With ActiveWorkbook
        wbname = Left(.Name, InStr(.Name, ".") - 1)
        .Sheets(1).Name = wbname
        .Close savechanges:=True
    End With
    MyFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sorry for bumping this thread. Is it possible for the above macro to be modified to work on all subfolders of the main folder? Have been trying to do it for a while now with know success.
Thank you
 
Upvote 0
If I wanted the first sheet in all workbooks to be renames to "Sheet1", will replacing the following line

.Sheets(1).Name = wbname

with

.Sheets(1).Name = Sheet1

work?
 
Upvote 0
Try this - change the folder name to suit

Rich (BB code):
Sub RenSheets()
Dim MyFolder As String
Dim MyFile As String
Dim wbname As String
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Application.ScreenUpdating = False
Do While MyFile <> ""
    Workbooks.Open Filename:=MyFolder & "\" & MyFile
    With ActiveWorkbook
        wbname = Left(.Name, InStr(.Name, ".") - 1)
        .Sheets(1).Name = wbname
        .Close savechanges:=True
    End With
    MyFile = Dir
Loop
Application.ScreenUpdating = True
End Sub
HI,

Can this be modified to rename several files in a folder to all have the same worksheet name "balance sheet"?
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,470
Members
449,384
Latest member
purevega

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