Rename sheet based on cell value

g3org

Board Regular
Joined
Jul 25, 2021
Messages
69
Office Version
  1. 365
Platform
  1. Windows
Hello guys,
Could someone please help me to modify this code to rename the sheet based on B3 if is nothing in B4? The value will be in B4 basically but if there is Empty, will be in B3.
Many thanks!

Sub tabname()
Dim ws As Worksheet
For Each ws In Worksheets
On Error Resume Next
If Len(ws.Range("B4")) >0 Then
ws.Name = Replace(ws.Range("B4").Value,"/","-")
End If
On Error Goto 0
If ws.Name <> Replace(ws.Range("B4").Value,"/","-") Then
Msgbox ws.Name & " Was Not renamed, the suggested name was invalid"
End If
Next
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi g3org,

Try this:

VBA Code:
Option Explicit
Sub tabname()
    
    Dim ws As Worksheet
    Dim strTabName As String
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
        On Error Resume Next
            strTabName = IIf(Len(ws.Range("B4").Value) = 0, ws.Range("B3").Value, ws.Range("B4").Value)
            ws.Name = Replace(strTabName, "/", "-")
        On Error GoTo 0
        If ws.Name <> Replace(strTabName, "/", "-") Then
            MsgBox ws.Name & " Was Not renamed, the suggested name was invalid"
        End If
    Next ws
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0
Solution

Forum statistics

Threads
1,214,812
Messages
6,121,702
Members
449,048
Latest member
81jamesacct

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