Rename part of sheet name vba

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Can someone help me figure out the code to rename just part of a sheet name while keeping the rest?

Current code is as follows but the original sheet name has a location description after the 4th character and would like to keep that. Plus this just tries to rename each sheet that contains A1 value to B1 value and runs into "The name is already taken error"

Thanks in advance for your assistance!

VBA Code:
Sub RenameSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    With ws
        If Left(.Name, 4) = Sheets("Lists").Range("A1").Value Then .Name = Left(Sheets("Lists").Range("B1").Value, 4)
    End With
Next ws
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi muhleebbin,

assuming both values are 4 characters try

VBA Code:
Sub RenameSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
  With ws
    If Left(.Name, 4) = Sheets("Lists").Range("A1").Value Then _
         .Name = Replace(.Name, Sheets("Lists").Range("A1").Value, Sheets("Lists").Range("B1").Value, 1)
  End With
Next ws
End Sub

Ciao,
Holger
 
Upvote 0
Solution
Such a simple solution!

Thank you Holger!
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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