Worksheet named based on cell value

alansbanans

New Member
Joined
Sep 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, first off I have gone through a number of posts that seem the same/similar to my request but the code will not work for me.

I have 33 worksheets that I would like to be renamed by the value in A2 on each of those sheets. Ex. If Sheet3 has 12345 in A2, I would like that sheet named “12345”.

I have tried a number of different forum answers including here and none of the code works for me.

Thanks so much in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Welcome to the Board!

Assuming you don't have any duplicates values in cell A2 across your sheets, and assuming you have valid characters in them, this should do it:
VBA Code:
Sub RenameSheets()

    Dim ws As Worksheet
    
'   Loop through all worksheets
    For Each ws In Worksheets
        If ws.Range("A2") <> "" Then
            ws.Name = ws.Range("A2")
        End If
    Next ws
    
End Sub
 
Upvote 0
Solution
Hi, first off I have gone through a number of posts that seem the same/similar to my request but the code will not work for me.

I have 33 worksheets that I would like to be renamed by the value in A2 on each of those sheets. Ex. If Sheet3 has 12345 in A2, I would like that sheet named “12345”.

I have tried a number of different forum answers including here and none of the code works for me.

Thanks so much in advance.
Give this a go. It checks for duplicates.

Run the subRenameWorksheets procedure.

It does not check for invalid worksheet names.

VBA Code:
Public Sub subRenameWorksheets()
Dim Ws As Worksheet
Dim strNames As String

  For Each Ws In ThisWorkbook.Worksheets
    
    With Ws
    
      If Not IsEmpty(.Range("A2").Value) Then
      
        If Not fncWorksheetExists(.Range("A2").Value) Then
      
          .Name = .Range("A2").Value
    
        Else
        
          strNames = strNames & vbCrLf & .Range("A2").Value & "  " & Ws.Name
          
        End If
      
      End If
    
    End With
  
  Next Ws
  
  MsgBox strNames, vbOKOnly, "Worksheets not renamed."
  
  ActiveWorkbook.Save

End Sub

Private Function fncWorksheetExists(strWorksheet As String) As Boolean
Dim Ws As Worksheet

  For Each Ws In ThisWorkbook.Worksheets
    
    If Ws.Name = strWorksheet Then
      
      fncWorksheetExists = True
      
      Exit Function
      
    End If
  
  Next Ws

End Function
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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