renaming worksheets problem with dup names

jek40

Active Member
Joined
Jan 17, 2005
Messages
317
i am using this code to rename worksheets based on a value in a cell on that sheet

Dim sname As String
Dim i As Integer
For i = 1 To 40
Sheets(i).Select
sname = Range("a1").Value
Sheets(i).Name = Format(sname, "mm-dd-yy")
Next i
End Sub

a problem arises because in the process of renaming the new sheet number 1-3 are the old sheets 38-40. (it will not work to leave 10 - 12 unchanged because data is being moved.)

any suggestions on an easy way to take care of this -- i was thinking of renaming the sheets to something generic and then renaming to the new names.

thanks
john
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi John,
Try this code
Code:
Sub RenameSheets()
Dim ws As Worksheet
Dim sname As String

For Each ws In Worksheets()
    ws.Select
    sname = Range("a1").Value
    ws.Name = Format(sname, "mm-dd-yy")
Next
End Sub
This code loops through all the worksheets in the workbook and rename all of them.

If you, for example, want to skip the first 3 sheets, use code like this :
Code:
Sub RenameSheets()

Dim ws As Worksheet
Dim sname As String

For Each ws In Worksheets()
    ws.Select
    If ws.Index > 3 Then
        sname = Range("a1").Value
        ws.Name = Format(sname, "mm-dd-yy")
    End If
Next
End Sub
edit : added the second code
 
Upvote 0
Thanks Bruno
if i want to change the names of sheets 4 through 40 and have a total of 50 sheets how would the code look for that?

thanks
John
 
Upvote 0
Code:
Sub RenameSheets()

Dim ws As Worksheet
Dim sname As String

For Each ws In Worksheets()
    ws.Select
    'which sheets needs a new name :
    If ws.Index > 3 And ws.Index < 41 Then
        sname = Range("a1").Value
        ws.Name = Format(sname, "mm-dd-yy")
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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