macro to change worksheet names

haganator

New Member
Joined
Aug 5, 2008
Messages
42
I'm running windows xp sp3 and using excel 2007.


There are 7 worksheets in this document, and each worksheet has a date for a name. Each week the worksheets are manually updated with the new dates. I would like to have a macro update these worksheets with the new dates. I don't know if it would be easier to use some type of calendar function, or if there is a way to use a pop-up message requesting the starting date to be used. Below is what the worksheet names look like.


Sub ChangeTabName()
'
' ChangeTabName Macro
'

'
Sheets("04-16-2012").Select
Sheets("04-16-2012").Name = "04-23-2012"
Sheets("04-17-2012").Select
Sheets("04-17-2012").Name = "04-24-2012"
Sheets("04-18-2012").Select
Sheets("04-18-2012").Name = "04-25-2012"
Sheets("04-19-2012").Select
Sheets("04-19-2012").Name = "04-26-2012"
Sheets("04-20-2012").Select
Sheets("04-20-2012").Name = "04-27-2012"
Sheets("04-21-2012").Select
Sheets("04-21-2012").Name = "04-28-2012"
Sheets("04-22-2012").Select
Sheets("04-22-2012").Name = "04-29-2012"

End Sub

Any help would be greatly appreciated.

Thanks,
Jeff
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You could try this;

Code:
Sub ChangeTabName() 'Adds 7 days to the sheet names that are dates

Dim Dte As Variant
Dim Sht As Worksheet

On Error Resume Next
    
    For Each Sht In ThisWorkbook.Worksheets
        Dte = Sht.Name
        If IsDate(Dte) Then Sht.Name = Format(DateAdd("d", 7, Dte), "mm-dd-yyyy")
    Next Sht

End Sub
 
Upvote 0
That worked beautifully. How could I use this code to change the sheet names if there is more than just the date? For example, how would the above code be used to change a sheet named "04-16-2012 smspm" to the next weeks date?

Thanks again for the first fix.
Jeff
 
Upvote 0
That worked beautifully. How could I use this code to change the sheet names if there is more than just the date? For example, how would the above code be used to change a sheet named "04-16-2012 smspm" to the next weeks date?

Thanks again for the first fix.
Jeff

Maybe like this, assuming the date is always the first part of the sheet name.

Code:
Sub ChangeTabName() 'Adds 7 days to the sheet names that are dates

Dim Dte As Variant
Dim Sht As Worksheet

On Error Resume Next
    
    For Each Sht In ThisWorkbook.Worksheets
        Dte = Left(Sht.Name, 10)
        If IsDate(Dte) Then
            Sht.Name = Format(DateAdd("d", 7, Dte), "mm-dd-yyyy") _
            & Mid(Sht.Name, 11, Len(Sht.Name) - 10)
        End If
    Next Sht

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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