Update to Macro - renaming worksheets according to text string in cell

Mtyler

Board Regular
Joined
Oct 13, 2006
Messages
62
Hi all

I've culled the following macro from a number of other sources to rename a sheet according to the text in cell C8 on that sheet

Sub Macro1()
z = Sheets.Count
For x = 1 To z
a = Sheets(x).Range("C8").Value
Sheets(x).Name = Left(a, 25)
Next
End Sub​

It works great. However, some of the data would lead to sheets being named identically, which throws out the macro. How can I update this macro so that it renames the second sheet with a (2) after the name?

In other words, gives me sheets called ABC Ltd and ABC Ltd (2) if two sheets have ABC Ltd in cell C8?

Thanks

Matt
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Not well tested, but:
Rich (BB code):
Option Explicit
    
Sub exa()
Dim wks As Worksheet
Dim i As Long
Dim strNewName As String
    i = 1
    For Each wks In ThisWorkbook.Worksheets
        strNewName = Left(wks.Cells(8, 3).Value, 25)
        Do While ShExists(strNewName)
            i = i + 1
            strNewName = Left(strNewName, 25) & Chr(32) & "(" & i & ")"
        Loop
        wks.Name = strNewName
    Next
End Sub
    
Function ShExists(ShName As String, _
                  Optional wb As Workbook, _
                  Optional CheckCase As Boolean = False) As Boolean
    
    If wb Is Nothing Then
        Set wb = ThisWorkbook
    End If
    
    If CheckCase Then
        On Error Resume Next
        ShExists = CBool(wb.Worksheets(ShName).Name = ShName)
        On Error GoTo 0
    Else
        On Error Resume Next
        ShExists = CBool(UCase(wb.Worksheets(ShName).Name) = UCase(ShName))
        On Error GoTo 0
    End If
End Function

Hope thta helps,

Mark
 
Upvote 0
[bleep]!

Stick the i=1 right below the For...Each
 
Upvote 0
Thanks for reply GTO - appears to give me a number behind most sheet names, rather than just those where the sheet name is duplicated. Any thoughts?

Thanks
 
Upvote 0
Hi Matt,

Sorry, you are right; the code I posted is far less than stellar. I've stared at this a couple of times, but brain-dead for some reason. I'll look tonight to see if you received some help.

Mark
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top