Dynamically Rename Worksheet Tabs

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
I am trying to create a macro to enable me to rename the tabs in a worksheet based on a cell in a different worksheet in the same workbook. The macro button will be located on the same sheet as C4.
I have a tab called Revised which has a year in cell C4.
I have 12 sheets Jan2018, Feb2018 etc...
I want to be able to rename the tabs when the year changes in C4.
I've been playing around with some code but haven't managed to get past a line or two without experiencing a type mismatch error when trying to populate a range. So not worth posting.

Any ideas would be very welcome.
 
Thanks for your solution JackDanIce.

I'm not sure what I was originally doing with the AND/OR Brackets scenario but it all works exactly as CJ said now. My Bad.

I converted this to run automatically when C4 was changed and placed the code in the Sheet Revised.
Code:
Private Sub worksheet_change(ByVal target As Range)

Dim yr As Integer, i As Integer
    yr = Sheets("Revised").Range("C4").Value
    For i = 1 To Worksheets.Count
        With Sheets(i)
            If Not .Name = "Revised" And Not .Name = "Comparison" And Not .Name = "ChartFriendly" And Not .Name = "Chart" And Not .Name = "DO_NOT_DELETE" Then
                .Name = Left(.Name, Len(.Name) - 4) & yr
            End If
        End With
    Next i
End Sub
But, under these conditions, when I run my other macros on this sheet it just keeps running until I press ESC. Then the action does complete successfully.
If I place the tab rename code in ThisWorkbook and run it from a button on the screen the other macros run fine. I don't know why this is. Any ideas?
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try:
Rich (BB code):
Private Sub worksheet_change(ByVal target As Range)


Dim yr As Integer, i As Integer
Application.EnableEvents = False
    yr = Sheets("Revised").Range("C4").Value
    For i = 1 To Worksheets.Count
        With Sheets(i)
            If Not .Name = "Revised" And Not .Name = "Comparison" And Not .Name = "ChartFriendly" And Not .Name = "Chart" And Not .Name = "DO_NOT_DELETE" Then
                .Name = Left(.Name, Len(.Name) - 4) & yr
            End If
        End With
    Next i
Application.EnableEvents = True
End Sub
Also, if using Excel 2007 or later, the compiler auto converts integers to longs, so generally integers are used less in VBA than long data types.
 
Upvote 0
This worked. I used the Application.EnableEvents in the code for my other macros rather than the rename tab code (above).
This has sped up the operation and it now completes without manual intervention.
The user now only has to populate the year in C4 rather than also have to run a macro.
Looks like we have a winner folks.

Thank you both for your help with this. That's the finishing touches to my little project.
Have an immensely good day.

Best Regards,
Sparky.
 
Upvote 0

Forum statistics

Threads
1,223,646
Messages
6,173,536
Members
452,520
Latest member
Pingaware

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