Changing a tab name in real-time based on cell value from another worksheet

yeshallfind

New Member
Joined
Aug 10, 2016
Messages
16
Hi guys. This sounds like a simple question and I've been searching and searching, but can't seem to find the right answer.

I currently have a spreadsheet with 2 tabs/worksheets named SHEET1 and SHEET2.

Basically all I want is if anything is entered in cell A1 of Sheet, then the tab on Sheet 2 name changes accordingly.

So A1 on sheet 1 is called APPLE, then tab on sheet 2 is also called APPLE.

and if So A1 on sheet 1 is called ORANGE, then tab on sheet 2 is also called ORANGE.

Hope this makes sense.

PS. There is a lot more to it, but I just want to get a grasp of this before asking my next question.

Thanks very much.
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If A1 gets its value from user entry, rather than from the result of a formula, putting this in the code module for Sheet1 should do what you want.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
        Sheet2.Name = CStr(Range("A1").Value)
    End If
End Sub
 
Upvote 0
Thank you very much mike. That works perfectly. However if I delete the contents of the cell, I get:

"Run-time error '1004'
Method 'Name' of object'_Worksheet' failed


I assume this is because the code does not state what name sheet 2 should be if cell A1 was blank?
 
Upvote 0
The value being entered in the cell results in the second sheet being assigned an invalid name.

This modified code should alert the user to that problem so they can fix it:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
        On Error GoTo BADNAME
        Sheet2.Name = CStr(Range("A1").Value)
        On Error GoTo 0
    End If
Exit Sub
BADNAME:
strA1 = [A1].Value
With Application
    .EnableEvents = False
    .Undo
    .EnableEvents = True
End With
MsgBox Chr(34) & strA1 & Chr(34) & " is an invalid sheet name!"
End Sub
 
Upvote 0
Thanks Jon.

Your code certainly does part of the job, but the problem I have is that sometimes I have to remove the data from A1 as it's not needed.

Is there a way instead that if I delete the data from cell A1, then excel renames that sheet back to "Sheet2" or back to whatever default name excel wants to choose ????
 
Last edited:
Upvote 0
Yes...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
        If [A1].Value = "" Then
            Sheet2.Name = "Sheet2"
        Else
            On Error GoTo BADNAME
            Sheet2.Name = CStr(Range("A1").Value)
            On Error GoTo 0
        End If
    End If
Exit Sub
BADNAME:
strA1 = [A1].Value
With Application
    .EnableEvents = False
    .Undo
    .EnableEvents = True
End With
MsgBox Chr(34) & strA1 & Chr(34) & " is an invalid sheet name!"
End Sub
 
Upvote 0
Jon you are just brilliant...this works perfect! Thanks so much! Also thanks again mick too.

I'm now going to move onto my next question if that's okay? It is in continuation of the previous questions, hence my post is continuing on here.

I have 2 worksheets:

Sheet1 (This is a blank worksheet)Template (This is a worksheet which has a basic table of data)


If I enter any text (let's call it APPLE) in Sheet1, cell A1, I want excel this time to create a new copy of the Template worksheet, but name the worksheet tab "APPLE"

So just as another example, Sheet1 cell A2 (PEAR) will create another copy of the template worksheet but name the tab "PEAR" and so on....


If I don't make any sense, then please let me know.
 
Upvote 0
Is this something you want in place of the previous requirement or somehow in addition to it?
 
Upvote 0
Jon, thank you very much for your reply. I've managed to resolve the issue, and really appreciate your help.

But I still do have another question.

Assuming there are 2 worksheets called SHEET1 and SHEET2.

If on SHEET1 (between A1 to A10):


A1 to A3 have some sort of text or number inside them, but A4 to A10 are blank.


How do I get SHEET 2 (any cell) to output the amount of cells which are NOT blank?
 
Upvote 0
Jon, thank you very much for your reply. I've managed to resolve the issue, and really appreciate your help.

But I still do have another question.

Assuming there are 2 worksheets called SHEET1 and SHEET2.

If on SHEET1 (between A1 to A10):


A1 to A3 have some sort of text or number inside them, but A4 to A10 are blank.


How do I get SHEET 2 (any cell) to output the amount of cells which are NOT blank?


Sorry, I forgot to add that the output should be something like Cells 2 out of 10
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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