Making Worksheet Copies for Daily Shifts

Written by Allen Wyatt (last updated January 25, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

Ryan has an Excel worksheet that he needs to use for each of 3 shifts every day. He makes a copy of this worksheet for each shift, each day, over and over. It seems to Ryan that it would be helpful if he had a macro that could copy the master worksheet 3 times for each day in a month, naming the worksheets "February 1 Shift 1," "February 1 Shift 2," etc. He can't find a macro to do something like this and was wondering if anyone could help.

This actually doesn't take that long to put together as a macro. The trick is to remember that the macro needs to copy your master worksheet 3 times for each day in the desired month. This also implies that the macro needs to determine how many days there are in the month. Once this is known, you can set up two nested For...Next loops to handle the actual creation process.

Sub CopyShiftSheets()
    Dim iDay As Integer
    Dim iShift As Integer
    Dim iNumDays As Integer
    Dim wMaster As Worksheet
    Dim sTemp As String

    iMonth = 2     ' Set to month desired, 1-12
    iCurYear = Year(Now())
    iNumDays = Day(DateSerial(iCurYear, iMonth + 1, 0))

    Set wMaster = Worksheets("Master") 'change to name of master

    For iDay = 1 To iNumDays
        For iShift = 1 To 3
            sTemp = MonthName(iMonth) & " " & iDay & " Shift " & iShift
            wMaster.Copy After:=Sheets(Sheets.Count)
            ActiveSheet.Name = sTemp
        Next iShift
    Next iDay
End Sub

Note that the desired month (in this case February) is assigned to the iMonth variable and that iCurYear is set to the current year. The number of days in that month and year is then calculated and stored in iNumDays.

The two For...Next loops go through each day and each shift, copying the Master worksheet and renaming it. When you are done, your workbook will have all the desired worksheets, named correctly. You'll want to be careful, however, that you don't run the macro twice for the same month. If you do, an error is generated because you will have duplicate-named worksheets in the workbook.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13730) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Converting Radians to Degrees

When applying trigonometry to the values in a worksheet, you may need to convert radians to degrees. This is done by ...

Discover More

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More

Understanding the COMPARE Field

The COMPARE field is rather esoteric, but it can be helpful when you need to compare two values using fields. The result ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Clearing the Undo Stack in a Macro

Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More

Adding Differently Formatted Text to a Cell

Want to add or replace some text in a column with text that is formatted differently? The ideas presented in this tip can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2020-01-27 06:16:32

David Robinson

An alternative approach to coding the month into the macro would be to have a sheet with a list of dates down column A, and have the macro loop through the used range of this sheet, stepping through it one row at a time and getting the date for each record to insert. This is very useful from a reuse point of view (you just enter different dates to add), and also gives you the option to include or exclude specific days, like weekends, holidays or shutdowns. For that matter, if you ever want to add a different number of shifts on different days, you could put number of shifts in column B and use that as the upper limit for iShift.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.