+ Reply to Thread
Results 1 to 3 of 3

Worksheet Tab Name = Cell Value

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2013
    Location
    Cambridge
    MS-Off Ver
    Win7 Office 2010
    Posts
    34

    Worksheet Tab Name = Cell Value

    Hi,

    I want to create a vba macro that will allow me to automatically rename a tab for a worksheet to the value that has been returned to a cell (a calculated value, not a value that I typed in myself). I have a massive worksheet and hence do not want to rename every worksheet.

    The following vba is what I am using at the moment but it does not return the "A5" cell value each time unless I retype the value in cell A5. The value in cell A5 is a value that has been returned via calculation hence I know I should have used worksheet_calculate, but this tends to execute infinitely.

    Please review the vba below and help me come up with a better way to update the tab name to cell value A5?

    Formula: copy to clipboard
    Private Sub Worksheet_Change(ByVal Target As Range)
    'Specify the target cell whose entry shall be the sheet tab name.
    If Target.Address <> "$A$5" Then Exit Sub
    'If the target cell is empty (contents cleared) then do not change the shet name
    If IsEmpty(Target) Then Exit Sub

    'If the length of the target cell's entry is greater than 31 characters, disallow the entry.
    If Len(Target.Value) > 31 Then
    MsgBox "Worksheet tab names cannot be greater than 31 characters in length." & vbCrLf & _
    "You entered " & Target.Value & ", which has " & Len(Target.Value) & " characters.", , "Keep it under 31 characters"
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Exit Sub
    End If

    'Sheet tab names cannot contain the characters /, \, [, ], *, ?, or :.
    'Verify that none of these characters are present in the cell's entry.
    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
    If InStr(Target.Value, (IllegalCharacter(i))) > 0 Then
    MsgBox "You used a character that violates sheet naming rules." & vbCrLf & vbCrLf & _
    "Please re-enter a sheet name without the ''" & IllegalCharacter(i) & "'' character.", 48, "Not a possible sheet name !!"
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    Exit Sub
    End If
    Next i

    'Verify that the proposed sheet name does not already exist in the workbook.
    Dim strSheetName As String, wks As Worksheet, bln As Boolean
    strSheetName = Trim(Target.Value)
    On Error Resume Next
    Set wks = ActiveWorkbook.Worksheets(strSheetName)
    On Error Resume Next
    If Not wks Is Nothing Then
    bln = True
    Else
    bln = False
    Err.Clear
    End If

    'If the worksheet name does not already exist, name the active sheet as the target cell value.
    'Otherwise, advise the user that duplicate sheet names are not allowed.
    If bln = False Then
    ActiveSheet.Name = strSheetName
    Else
    MsgBox "There is already a sheet named " & strSheetName & "." & vbCrLf & _
    "Please enter a unique name for this sheet."
    Application.EnableEvents = False
    Target.ClearContents
    Application.EnableEvents = True
    End If

    End Sub

  2. #2
    Registered User
    Join Date
    08-08-2013
    Location
    Cambridge
    MS-Off Ver
    Win7 Office 2010
    Posts
    34

    Re: Worksheet Tab Name = Cell Value

    Oh and to help I also have this code (see below) but it almost works for me, but not quite – the sheet tab names I want changed (‘target’ sheets) depend on a selection in a ‘main’ sheet. So, after putting the above code in a target sheet whose tab name I need changed, I go to the ‘main’ sheet to a drop down list to choose the required selection, which then automatically makes a change in cell A1 of the target sheets – as the code below wrote works on the ActiveSheet, the code changes the tab name of my main sheet (as this is where I go to make my selection which activates the change in A1 of the target sheet based on a formula), rather than my target sheet. Is there any way you can amend the above code so that my target sheet tab name changes rather than my main sheet tab name? I’m very new with VBA, so if you could list out the exact changes that I would need to make, that would be greatly appreciated! Thank you.


    Formula: copy to clipboard
    Private Sub Worksheet_Calculate()
    With Range("A5")
    If Len(.Value) = 0 Or Len(.Value) > 31 Then Exit Sub

    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
    If InStr(.Text, (IllegalCharacter(i))) > 0 Then
    MsgBox "The formula in cell A5 returns a value containing a character that violates sheet naming rules." & vbCrLf & _
    "Recalculate the formula without the ''" & IllegalCharacter(i) & "'' character.", _
    48, "Not a possible sheet name !!"
    Exit Sub
    End If
    Next i

    Dim strSheetName As String, wks As Worksheet, bln As Boolean
    strSheetName = (.Text)
    On Error Resume Next
    Set wks = ActiveWorkbook.Worksheets(strSheetName)
    On Error Resume Next
    If Not wks Is Nothing Then
    bln = True
    Else
    bln = False
    Err.Clear
    End If

    If bln = False Then
    ActiveSheet.Name = strSheetName
    ElseIf ActiveSheet.Name <> .Text Then
    MsgBox "There is already a sheet named " & strSheetName & "." & vbCrLf & _
    "Recalculate the formula in cell A5 to return a unique name."
    End If

    End With
    End Sub

  3. #3
    Registered User
    Join Date
    08-08-2013
    Location
    Cambridge
    MS-Off Ver
    Win7 Office 2010
    Posts
    34

    Re: Worksheet Tab Name = Cell Value

    Anybody?...........................

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] copy paste from worksheet to other worksheet from cell B1 to cell A1
    By rickmeister in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2013, 04:07 AM
  2. Macro to select worksheet based on cell value and then return data to diff worksheet
    By clnossok in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2013, 07:48 PM
  3. Making link to one worksheet's cell to another worksheet's cell
    By talented77 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-10-2013, 10:40 PM
  4. Copy Cell In Adjacent Worksheet To Same Cell In Active Worksheet
    By az-man in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2007, 05:32 PM
  5. Replies: 2
    Last Post: 05-03-2007, 11:11 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1