Changing a tab name in real-time based on cell value from another worksheet
Amazing chart utilities from Jon Peltier
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

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

  1. #1
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

     
    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 by yeshallfind; Feb 11th, 2018 at 02:37 PM.

  2. #2
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    21,457
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

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

    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

  3. #3
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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?

  4. #4
    New Member JonXL's Avatar
    Join Date
    Feb 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  5. #5
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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 by yeshallfind; Feb 11th, 2018 at 03:50 PM.

  6. #6
    New Member JonXL's Avatar
    Join Date
    Feb 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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

  7. #7
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

  8. #8
    New Member JonXL's Avatar
    Join Date
    Feb 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Is this something you want in place of the previous requirement or somehow in addition to it?

  9. #9
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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?

  10. #10
    New Member
    Join Date
    Aug 2016
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

      
    Quote Originally Posted by yeshallfind View Post
    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

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com