+ Reply to Thread
Results 1 to 6 of 6

tab=cell name Question....

Hybrid View

  1. #1
    Registered User
    Join Date
    08-23-2012
    Location
    Haleyville,Alabama
    MS-Off Ver
    Excel 2010
    Posts
    65

    tab=cell name Question....

    Thanks for looking.. I found the code below that names the current sheet tab the data which is in A1. I was wandering if there is away for this to work for the entire workbook without having to put the code in each sheet.

    Private Sub Worksheet_Change(ByVal Target As Range)
        'Specify the target cell whose entry shall be the sheet tab name.
        If Target.Address <> "$A$1" 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
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,024

    Re: tab=cell name Question....

    Sub ChangeWorkSheetName()
    Dim WS As Worksheet
    For Each WS In Worksheets
    WS.Name = WS.Range("A1")
    Next
    End Sub
    Try the above
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    08-23-2012
    Location
    Haleyville,Alabama
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: tab=cell name Question....

    I tried putting your code in "ThisWorkbook" and also I tried putting it in sheet 1 but had no luck. What may I be doing wrong?? Thanks for you help

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,024

    Re: tab=cell name Question....

    Put in a new module and run it as a macro. It is not a worksheet or workbook event.

    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button

  5. #5
    Registered User
    Join Date
    08-23-2012
    Location
    Haleyville,Alabama
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: tab=cell name Question....

    Thanks for the Help

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: tab=cell name Question....

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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. Question IF Cell X =Win then Cell Y = running total
    By BACONbits24 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-23-2013, 01:12 PM
  2. Replies: 3
    Last Post: 01-23-2013, 10:24 PM
  3. Replies: 2
    Last Post: 05-24-2010, 10:13 AM
  4. [SOLVED] Another cell formatting dependent on cell contents question / message box popup?
    By StargateFan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-14-2006, 10:50 AM
  5. Replies: 0
    Last Post: 03-06-2005, 03:10 PM

Tags for this Thread

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