Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Aug 2002
    Location
    Michigan, USA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change Worksheet Names by Function/VBA? (2000)

    Hello everyone. Does anyone know if it is possible to use a function or VBA code to dynamically change the name of a worksheet tab (you know, the tabs at the bottom of excel)? Please let me know if anyone has discovered a way to do this, as I can't seem to find anything out there... Thank you for your time and insight!

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    You cannot do by use of a function, but it couyld be done by VBA. If you could you advise the event that will trigger the name change, I am sure somebody will show you the code to achieve it.

    Andrew C

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    The following code will change the name of the first worksheet in the workbook to "MySheet". This can not be done in a function, only in a Sub Procedure.

    <pre> Worksheets(1).Name = "MySheet"
    </pre>

    Legare Coleman

  4. #4
    Star Lounger
    Join Date
    Aug 2002
    Location
    Michigan, USA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    Thank you for that info - much appreciated. How could I rename a worksheet that I am currently using. For example, the worksheet is called "Sheet 4" but depending on what the user inputs into a certain cell, I would like to rename the current worksheet to something else. Thank you for your feedback...!!!

  5. #5
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    Something like:

    <pre>activesheet.Name=range("a1").Value</pre>


    or with a dialog box
    <pre>Sub RenameSheet()
    Dim Newname As String
    Newname = InputBox("Name for new worksheet?")
    If Newname <> "" Then
    ActiveSheet.Name = Newname
    End If
    End Sub
    </pre>


    If you want some error checking to make sure it is valid
    <pre>Sub RenameSheet2()
    On Error Resume Next

    ActiveSheet.Name = InputBox("Name for new worksheet?")

    'Keep asking for name if name is invalid
    Do Until Err.Number = 0
    Err.Clear
    ActiveSheet.Name = InputBox("Try Again!" _
    & vbCrLf & "Invalid Name or Name Already Exists" _
    & vbCrLf & "Please name the New Sheet")
    Loop
    On Error GoTo 0

    End Sub
    </pre>


    Steve

  6. #6
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    If you want to do it "automagically", you should use a Worksheet_Change event. See Sams <post#=164155>post 164155</post#> for how to set up a Worksheet_Change event, and Legare's example code for another purpose in this <post#=163547>post 163547</post#>, and Steve's post in this thread. You should end up with something like this:

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, ActiveSheet.Range("A1")) Is Nothing Then
    Application.EnableEvents = False
    ActiveSheet.Name = ActiveSheet.Range("A1").Value
    Application.EnableEvents = True
    End If
    End Sub
    -John ... I float in liquid gardens
    UTC -7ąDS

  7. #7
    2 Star Lounger
    Join Date
    Sep 2001
    Location
    Blue Springs, Missouri, USA
    Posts
    108
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    I already use something similar where a user inputs a value into a cell that is named "Name"

    Sub TabName()
    Application.ScreenUpdating = False
    Application.Goto reference:="Name"
    NewTabName = ActiveCell.Value
    ActiveSheet.Name = NewTabName
    Application.Goto reference:="R1C1"
    Application.ScreenUpdating = True
    End Sub

    Dennis

    <img src=/S/gramps.gif border=0 alt=gramps width=20 height=20>

  8. #8
    Star Lounger
    Join Date
    Aug 2002
    Location
    Michigan, USA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    Everyone, many, many, many thanks for your time and your tips. These solutions certainly hold the answer I was looking for - I really appreciate it.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    This can be shortened tremendously when you don't select (hardly ever necessary):

    Sub TabName()
    ActiveSheet.Name = ActiveWorkbook.activesheet.range("Name")
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  10. #10
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    Just a note and opinion:
    Much of the code for changing the name posted, does NOT have any error checking. Renaming a sheet is more prone to have an error than many other types of code you might create:

    Names must be <32 characters long
    They can NOT be blank
    They can NOT contain any of the following:
    / ? * [ ]

    You should at least "trap the error", keep the old name, and send a message to the user.

    I would NOT want my users getting a "crashed" macro, especially when you are automating a task that is essentially a beginner's level task (dbl-click on the tab and type a new name).

    Steve

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Worksheet Names by Function/VBA? (2000)

    Of course I agree with your remarks.

    I tend to write replies with the idea that the code will be used by the poster him/herself. Error checking is less important in those situations.
    Only when they get back to me with remarks like "in this-or-that situation the code errors out, why? " I dive into error checking.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

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