+ Reply to Thread
Results 1 to 16 of 16

Rename Sheets in a Workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Rename Sheets in a Workbook

    Hi Everyone,
    the following code gives all worksheets in a workbook the name of the string found in cell A1.

    But I have the problem that I have sheets which have only one word. Then the script does not work anymore.

    Also I would like to have the complete content of cell A1 as sheet name no matter what is in it.

    Maybe you can help me to adjust the code to use all characters in cell A1 as sheet names of the respective sheet.

    Here is the Code:
    Sub RenameWorksheet_Content_A1_2_Words()
    
    Dim WS As Worksheet
    Dim shtName
    Dim newName As String
    Dim i As Integer
    
    For Each WS In Worksheets
    With WS
    If Trim(.Range("A1")) <> "" Then
    shtName = Split(Trim(.Range("A1")), " ")
    newName = shtName(0) & " " & shtName(1)
    On Error GoTo ws_name_error
    .Name = newName
    GoTo done
    repeat:
    .Name = newName & i
    GoTo done
    ws_name_error:
    i = i + 1
    Resume repeat
    End If
    End With
    On Error GoTo 0
    done:
    Next
    End Sub

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Rename Sheets in a Workbook

    the reason it doesn't work is because you spec'd the delimiter as a space using split(). that function only does one thing.
    you can't make it do anymore. the best workaround, IMO, is to create sheets that do you not have spaces in them. that is, after all,
    protocol for all developers anyway. spaces create very bad issues. that's why SEO addresses on websites do not have spaces in them.
    they have dashes (-) or underscores (_).
    Attached Images Attached Images

  3. #3
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Rename Sheets in a Workbook

    What's wrong with just using this line?
    newName = WS.Range("A1").Value
    So this would be your entire code..
    Sub RenameWorksheet_Content_A1_2_Words()
    
    Dim WS As Worksheet
    Dim shtName
    Dim newName As String
    Dim i As Integer
    
    For Each WS In Worksheets
    With WS
    newName = WS.Range("A1").Value
    On Error GoTo ws_name_error
    .Name = newName
    GoTo done
    repeat:
    .Name = newName & i
    GoTo done
    ws_name_error:
    i = i + 1
    Resume repeat
    
    End With
    On Error GoTo 0
    done:
    Next
    End Sub
    Last edited by Croweater; 11-29-2020 at 06:43 PM.

  4. #4
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    Thank you for your help:
    It gives me an error in this line:

    i = i + 1

    And there is a Problem with sheetnames with only one word.
    Is there a chance that you can add underscores (_) in the sheetname.
    I would not mind that.
    But I am just a layman.

    As an alternative solution 2 words should also be enough. But I also have texts in A1, which contain only one word.
    And then the vb a doesn't work no more.

    Thank you for your help.
    Last edited by CoSinus; 11-30-2020 at 08:01 AM.

  5. #5
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Rename Sheets in a Workbook

    Quote Originally Posted by CoSinus View Post
    Thank you for your help:
    It gives me an error in this line:

    i = i + 1
    What is the error? Can you post a workbook to demonstrate that error?


    And there is a Problem with sheetnames with only one word.
    Did you take out the line where you are trying to SPLIT the cell using the space (as I posted above)?
    If you copy the code I pasted this should not be a problem.

  6. #6
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    @jindon:

    Here is an error in
    WS.Name = Replace(WS.[a1], " ", "_")
    I send you the excel file.

    As you can see in the sample Sheet the Sheetnames do not carry the strings in A1.

    Thanks all for your help so much.
    Attached Files Attached Files
    Last edited by CoSinus; 11-30-2020 at 10:59 AM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,677

    Re: Rename Sheets in a Workbook

    Some possible error for renaming sheet.

    1) Can't use the name that already exists in the workbook.
    2) Length must be less than 31 characters.
    3) Use of invalid characters i.e. : ? \ [ ] / *

    If you don't care about above then simply
    Sub RenameWorksheet_Content_A1_2_Words()
        Dim WS As Worksheet
        For Each WS In Worksheets
            If WS.[a1] <> "" Then WS.Name = Replace(WS.[a1], " ", "_")
        Next
    End Sub

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,677

    Re: Rename Sheets in a Workbook

    As I mentioned already, length...
    Sub RenameWorksheet_Content_A1_2_Words()
        Dim WS As Worksheet
        For Each WS In Worksheets
            If WS.[a1] <> "" Then WS.Name = Replace(LEFT(WS.[a1],30), " ", "_")
        Next
    End Sub

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,520

    Re: Rename Sheets in a Workbook

    Some of the A1 cells have char > 31...
    Time Spent on Decisions by Teams = 32 Char

    Sub sintekJ3v16()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        ws.Name = Left(ws.Range("A1").Value, 31)
    Next ws
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  10. #10
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    Thank you all for your help.
    The solution in the end is perfect.

    I am very grateful.

  11. #11
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    I know that the thread was closed,
    but I have worked on a code with your help that could deal with the problem of the string length.
    The Code has flaws. Could you please help me.

    To test it I attached a document that has too long names in A! on some sheets which are partly the same.

    The goal of the code should be to cut off the characters and to add a numbering in case of duplicate designations, so that no error message appears.

    I am glad about your help to correct the code.

    Thanks a lot

    Sub RenameWorksheet_Content_A1_ALL_Words()
    
    Dim ws As Worksheet
    Dim shtName
    Dim newName As String
    Dim i As Integer
    
    For Each ws In Worksheets
    With ws
     shtName = ws.Cells(1, 1).Value
       If Len(s) > 0 Then
         If Len(s) > 31 Then
             shtName = Left(s, 28) & "..."
             End If
          ws.Name = shtName
    On Error GoTo ws_name_error
    .Name = newName
    GoTo done
    repeat:
    .Name = newName & i
    GoTo done
    ws_name_error:
    i = i + 1
    Resume repeat
    End If
    End With
    On Error GoTo 0
    done:
    Next
    End Sub
    Attached Files Attached Files
    Last edited by CoSinus; 12-01-2020 at 08:06 AM.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,677

    Re: Rename Sheets in a Workbook

    This is atypical human error when you did not enable Option Explicit to force the variables to be declared.
    If you add Option Explicit at the top of the module, it will tell you the variable(s) that are not declared.

  13. #13
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    Thanks for the Tip.
    I did it and made changes.
    I guess it is almost working.

    Here is the revised vba:

    Sub RenameWorksheet_Content_A1_ALL_Words()
    Dim ws As Worksheet
    Dim shtName As String
    Dim i As Integer
    
    For Each ws In Worksheets
    With ws
     shtName = ws.Cells(1, 1).Value
       If Len(shtName) > 0 Then
         If Len(shtName) > 31 Then
             shtName = Left(shtName, 27) & "..."
             End If
          ws.Name = shtName
    On Error GoTo ws_name_error
    GoTo done
    repeat:
    ws.Name = shtName & i
    GoTo done
    ws_name_error:
    i = i + 1
    Resume repeat
    End If
    End With
    On Error GoTo 0
    done:
    Next
    End Sub
    Small error with
    i = i + 1
    Maybe someone can check please.
    Thanks everybody!
    Last edited by CoSinus; 12-01-2020 at 01:05 PM.

  14. #14
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,520

    Re: Rename Sheets in a Workbook

    Play around with this...
    Sub sintekJ3v16()
    Dim Ws As Worksheet, Num As Long, Str As String
    For Each Ws In ThisWorkbook.Sheets
        With Ws
            If Len(.Range("A1")) > 31 Then Str = Left(.Range("A1").Value, 27) Else Str = .Range("A1")
            If Evaluate("ISREF('" & Str & "'!A1)") = True Then Str = IIf(Len(Str) > 29, Left(Str, 28) & "-" & Num + 1, Str & "-" & Num + 1)
            .Name = Str
        End With
    Next Ws
    End Sub

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,677

    Re: Rename Sheets in a Workbook

    CoSinus

    Refer to my Post#5 for Error renaming sheet.
    Sub RenameWorksheet_Content_A1_ALL_Words()
        Dim ws As Worksheet
        For Each ws In Worksheets
            ws.Name = GetNewWSName(ws.Cells(1, 1).Value, ws.Name)
        Next
    End Sub
    
    Function GetNewWSName(ByVal wsName As String, myName As String) As String
        Dim i As Long, temp As String
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "[:?\\\[\]/*]+"
            wsName = .Replace(wsName, "")
        End With
        wsName = Left$(wsName, 31)
        If wsName = myName Then GetNewWSName = myName: Exit Function
        If Evaluate("isref('" & wsName & "'!a1)") Then
            wsName = Left$(wsName, 27)
            Do
                i = i + 1
                temp = wsName & Format$(i, "_000")
                If (temp = myName) + (Not Evaluate("isref('" & temp & "'!a1)")) Then
                    GetNewWSName = temp: Exit Do
                End If
            Loop
        Else
            GetNewWSName = wsName
        End If
    End Function

  16. #16
    Forum Contributor
    Join Date
    05-24-2020
    Location
    London
    MS-Off Ver
    Office 365 pro
    Posts
    177

    Re: Rename Sheets in a Workbook

    Thank you very much.
    I have been looking for a solution to this problem for so long.

    THANKS!!!

+ 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] macro to rename all sheets in workbook as criteria cells A2 of each sheet
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-13-2019, 04:44 PM
  2. [SOLVED] Loop worksheets and if sheets Name matches pattern, rename sheets
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-23-2016, 01:08 AM
  3. [SOLVED] Copy a sheet, rename it by value in range then export product to new workbook and rename
    By MagicMan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-31-2015, 07:24 PM
  4. Replies: 0
    Last Post: 02-06-2015, 04:50 PM
  5. copy sheets to new workbook with new name, rename tabs within that workbook
    By Conky4 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-12-2012, 04:44 PM
  6. [SOLVED] Code to create new sheet and rename, regardless of any other sheets in workbook
    By csh8428 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-19-2012, 02:22 PM
  7. Code to delete sheets and rename sheets in a protected workbook?
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-07-2011, 03:02 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