+ Reply to Thread
Results 1 to 7 of 7

Create worksheets for weeks between and including first and last Mon of two month period

  1. #1
    Forum Contributor
    Join Date
    10-08-2013
    Location
    London
    MS-Off Ver
    365
    Posts
    136

    Create worksheets for weeks between and including first and last Mon of two month period

    Hi All,

    I'm nearing the end of creating a staff schedule template that will allow users to create a bi-monthly staff scheduling workbook that is broken down into a separate worksheet for each week within a two month period. I have created a user form that is called when the 'Schedule Master Template' (attached) is opened. The user form has two combo boxes - one to select a year for the schedule and the second to select the pair of months the schedule is for - and a command button to run the macro that creates the workbook.

    It is the macro that creates the schedule from the user form command button click that I need assistance with. I'll try to describe the requirements below.

    Userform ComboBox1 selections are: Please Select, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026

    Userform ComboBox2 selections are: Please Select, January/February, March/April, May/June, July/August, September/October, November/December

    Userform CommandButton1 is to create the schedule.

    Using the selections for year and month pairings in the two combo boxes as references, I am hoping to populate cell E2 on sheet 3 with the first Monday of the month and cells H2, K2, N2, T2 and W2 with the next six consecutive days. For example, (as per the TEST attachment) for selections 2015 and March/April, E2 will be 2nd March (first Monday of the month) in the format MONDAY 02 MAR or Monday 02 Mar, and and cells H2, K2, N2, T2 and W2 will be populated with the next 6 days. The sheet should then be named with the first and last day on the sheet + the month. Format for this sheet first sheet should show '02-08Mar'.

    The macro should then create further sheets for the next number of weeks until the last week containing the last Monday of the second month has been reached, naming each day within each worksheet at E2, H2, K2, N2, T2 and W2 and naming the sheet as it progresses.

    The 'TEST - 02_Schedule_Mar_Apr_2015' attachment has what the finished schedule would look like. Please ignore errors within the workbook as this is a work in progress and could also be due to me removing sensitive data.

    The biggest hurdle I have is having the macro recognise the first and last Monday of a two month set when creating new worksheets. If someone could assist with code to create the necessary worksheets from the userform data selections, I could probably sort out the naming of the days and worksheets myself.

    Any help would be greatly appreciated!

    Regards,
    Attached Files Attached Files
    Dave C

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Create worksheets for weeks between and including first and last Mon of two month peri

    Hi Dave,

    If you are still watching this thread I wrote you some code to get you started.

    The first is some code for the button that will call a couple of functions to get you the first and last Mondays based on what your users pick.

    The second is the functions themselves.

    Right click the command button, then view code and paste this code there.
    Please Login or Register  to view this content.

    Go to insert, then choose module and paste this code there.
    Please Login or Register  to view this content.
    Last edited by skywriter; 01-25-2015 at 10:25 PM.

  3. #3
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Create worksheets for weeks between and including first and last Mon of two month peri

    Here's what I have so far.
    See if you like it.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    10-08-2013
    Location
    London
    MS-Off Ver
    365
    Posts
    136

    Re: Create worksheets for weeks between and including first and last Mon of two month peri

    Hi skywriter,

    Firstly, many, many thanks for your assistance with this and secondly, sorry for the late reply! I've never used functions in VBA before but I see the direction you've headed with them even if I don't fully understand at this stage.

    I tried opening the file you attached, selected a year and month set and once the command button was clicked received an error - Runtime error ‘32809’ Application-defined or object-defined error. The Copy Sheets Sub is quite simple but I just can't see why Sheet 3 was not recognised (if this is the cause). I did notice that there is an extra worksheet, 'Sheet1', already added which did not appear to serve a purpose but deleting this from the template didn't help resolve the error.

    Do you know what might be happening with the runtime error? My profile says I use Excel 2013 which is when I'm at work but I occasionally use my personal laptop which runs 2010. Clutching at straws here but could this be a reason?

    Thanks again for your start on this macro.

  5. #5
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Create worksheets for weeks between and including first and last Mon of two month peri

    My first question would be did you open the actual sheet and run this from the sheet I posted, or did you copy the code and put it into another sheet?

    When the error comes up one of the choices you would have is a button labeled debug, if you push that button it will take you to the code with a line highlighted in yellow. I would like to know what that line is.

  6. #6
    Forum Contributor
    Join Date
    10-08-2013
    Location
    London
    MS-Off Ver
    365
    Posts
    136

    Re: Create worksheets for weeks between and including first and last Mon of two month peri

    Hi skywriter, yes I did open your attachment and this is where I encountered the error:
    Please Login or Register  to view this content.
    I'm ok at debugging but I'm still a bit baffled as to why this line raised an error. This aside I have now applied your code to my own sheet and saw how you have pointed me in the right direction with the Msgboxes. I then added the CopySheets Sub and also the Private Sub CommandButton1_Click() as per your attachment, reopened the new workbook and can say that you have absolutely nailed what I was trying to achieve. Thank you very much! I have begun to add further functionality to the macro to complete the template such as formatting headings as per below.
    Please Login or Register  to view this content.
    Even though I missed it first time round, thanks for demonstrating the method the way you did. It was extremely helpful!

    Regards,

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Cool Re: Create worksheets for weeks between and including first and last Mon of two month peri

    Quote Originally Posted by L plates View Post
    Hi skywriter, yes I did open your attachment and this is where I encountered the error:
    Please Login or Register  to view this content.
    I'm ok at debugging but I'm still a bit baffled as to why this line raised an error. This aside I have now applied your code to my own sheet and saw how you have pointed me in the right direction with the Msgboxes. I then added the CopySheets Sub and also the Private Sub CommandButton1_Click() as per your attachment, reopened the new workbook and can say that you have absolutely nailed what I was trying to achieve. Thank you very much! I have begun to add further functionality to the macro to complete the template such as formatting headings as per below.
    Please Login or Register  to view this content.
    Even though I missed it first time round, thanks for demonstrating the method the way you did. It was extremely helpful!

    Regards,
    I'm glad it worked out and thanks for the reputation points.
    Good luck!!! My pleasure.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 10
    Last Post: 01-09-2014, 07:04 PM
  2. Get maximum frequency in 9 weeks rolling period
    By baltblue in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-15-2013, 11:37 AM
  3. Replies: 0
    Last Post: 07-30-2013, 09:01 AM
  4. Count number of networkdays for each month in a multi month period
    By dreddster in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-10-2013, 11:12 AM
  5. [SOLVED] Need help with a formula to divide allotted hours over a period of weeks.
    By Irishmatty513 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-01-2013, 07:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1