Change tab name If:

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,338
Office Version
  1. 365
Platform
  1. Windows
How do I modify this so that is cell R2 has the word Template in it, the code does not change the tab name?
Code:
Private Sub Worksheet_Deactivate()
'Updateby20150602
Dim Ws As Worksheet
Dim rngAddr As String
Dim Name As String
rngAddr = "R1"
For Each Ws In Application.ActiveWorkbook.Sheets
Name = Ws.Range(rngAddr).Value
If Name <> "" Then
Ws.Name = Name
End If
Next

End Sub

Thanks!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
try this
VBA Code:
Private Sub Worksheet_Deactivate()
    Dim Ws As Worksheet
    Dim rngAddr As String
    Dim Name As String
    rngAddr = "R1"
    For Each Ws In Application.ActiveWorkbook.Sheets
        Name = Ws.Range(rngAddr).Value
            If Name <> "" And LCase(Ws.Range("R2")) <> "template" Then
                Ws.Name = Name
            End If
    Next
End Sub
 
Upvote 0
Thank you

What if I didn't want to check and rename every worksheet and just wanted to do this for active worksheet?
 
Upvote 0
Is Worksheet_Deactivate where you plan to use it?
VBA Code:
Private Sub Worksheet_Deactivate()
    Dim shName As String
    shName = Range("R1")
    If Name <> "" And LCase(Range("R2")) <> "template" Then Me.Name = shName
End Sub

Otherwise
VBA Code:
Sub TestShName()
    Dim shName As String, ws As Worksheet
    Set ws = ActiveSheet
    shName = ws.Range("R1")
    If Name <> "" And LCase(ws.Range("R2")) <> "template" Then ws.Name = shName
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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