VBA Copy and Rename 2 Worksheets with current date but different shifts.

Jeff S

New Member
Joined
Apr 6, 2023
Messages
5
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
  2. Web
Hello,
Having issues with 1 button macro on a menu worksheet that copies 2 separate worksheets and places a cell value on them.
Example:
1680782718694.png

It would be executed from Staff Summary Sheet and copy then rename the Working Sheets.
I get it to copy but I somehow mess up with the renaming part.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the Board!

Please post your VBA code for us to analyze.
Note that there are instructions for how to post your VBA code here: How to Post Your VBA Code
 
Upvote 0
Thanks Joe, sorry about that. Here it is, but still messes up at the first active sheet. The Msg boxes are in for me to see the current active sheet.. It helped on an earlier problem I had, but fixed. Not required now.

Rich (BB code):
Sub TABCOPY1()
'
' TABCOPY1 Macro
'
   
'
    Sheets("Gold-WORKING").Select
    Sheets("Gold-WORKING").Copy Before:=Sheets(1)
    MsgBox "The name of the active sheet is " & ActiveSheet.Name
    ActiveSheet.Name = ActiveSheet.Range("E1").Value
        Sheets("Blue-WORKING").Select
        Sheets("Blue-WORKING").Copy Before:=Sheets(1)
        MsgBox "The name of the active sheet is " & ActiveSheet.Name
        ActiveSheet.Name = ActiveSheet.Range("E1").Value
            Sheets("Staff Summary").Select
End Sub
 
Upvote 0
OK, what exactly is in cell E1?

Can you walk us through a simple example, explaining exactly what you want to happen, and telling us what actually is happening for that particular example?
 
Upvote 0
OK, what exactly is in cell E1?

Can you walk us through a simple example, explaining exactly what you want to happen, and telling us what actually is happening for that particular example?
The manager will update the Staff Summary Sheet and at the end of the week will click on the macro button. I provide the macro to take a snapshot (copy) of the staffing for both shifts Gold and Blue. I have formatted the cells (E1) on both shifts Tabs to give the shift "Blue-" or "Gold-" then the start of the week mmm-dd-yyyy. They are doing this manually (Thus the picture), but would like an easier method.
 
Upvote 0
Here is code that will automatically use today's date without any input from the user:
VBA Code:
Sub TABCOPY1()
'
' TABCOPY1 Macro
'
    Dim shtPfx As String
    Dim arr() As String
    Dim newName As String
    
    Sheets("Gold-WORKING").Select
    
'   Build new sheet name by getting prefix and adding current date
    arr = Split(ActiveSheet.Name, "-")
    newName = arr(0) & Format(Date, "-m-d-yy")

    Sheets("Gold-WORKING").Copy Before:=Sheets(1)
    ActiveSheet.Name = newName

End Sub

You mentioned "Start of the week", but then used date 4-4-23, which is a Tuesday.
If you can define actually when the "Start of the week" happens, we can probably amend the code to do that.
 
Upvote 0
Here is code that will automatically use today's date without any input from the user:
VBA Code:
Sub TABCOPY1()
'
' TABCOPY1 Macro
'
    Dim shtPfx As String
    Dim arr() As String
    Dim newName As String
   
    Sheets("Gold-WORKING").Select
   
'   Build new sheet name by getting prefix and adding current date
    arr = Split(ActiveSheet.Name, "-")
    newName = arr(0) & Format(Date, "-m-d-yy")

    Sheets("Gold-WORKING").Copy Before:=Sheets(1)
    ActiveSheet.Name = newName

End Sub

You mentioned "Start of the week", but then used date 4-4-23, which is a Tuesday.
If you can define actually when the "Start of the week" happens, we can probably amend the code to do that.
The manager when copying it started his week on the 4th and copied it that way by accident. It should be every Monday even if a holiday.
Sorry for confusion. I just started this position in Jan 23 and trying to remember VBA coding from 2 decades ago in an Auto Plant is testing my knowledge and memory limits.
 
Upvote 0
OK, try this version:
VBA Code:
Sub TABCOPY1()
'
' TABCOPY1 Macro
'
    Dim shtPfx As String
    Dim arr() As String
    Dim newName As String
    
    Sheets("Gold-WORKING").Select
    
'   Build new sheet name by getting prefix and adding current date
    arr = Split(ActiveSheet.Name, "-")
    newName = arr(0) & Format(Date - (7 - Weekday(Date, vbMonday)), "-m-d-yy")

    Sheets("Gold-WORKING").Copy Before:=Sheets(1)
    ActiveSheet.Name = newName

End Sub
 
Upvote 0
Solution
OK, try this version:
VBA Code:
Sub TABCOPY1()
'
' TABCOPY1 Macro
'
    Dim shtPfx As String
    Dim arr() As String
    Dim newName As String
   
    Sheets("Gold-WORKING").Select
   
'   Build new sheet name by getting prefix and adding current date
    arr = Split(ActiveSheet.Name, "-")
    newName = arr(0) & Format(Date - (7 - Weekday(Date, vbMonday)), "-m-d-yy")

    Sheets("Gold-WORKING").Copy Before:=Sheets(1)
    ActiveSheet.Name = newName

End Sub
That did it, now I repeat for Blue and that should fix it. I would never have remembered that. Thanks Joe. I tried so many ways to make this work and remember doing it years ago. Old age I guess. Your a life saver. Thank you!
 
Upvote 0
You are welcome!
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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