+ Reply to Thread
Results 1 to 7 of 7

Splitting single column of dates to 2 columns representing a beginning and end date

  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Splitting single column of dates to 2 columns representing a beginning and end date

    Hi Guys,

    I have 3 columns in a workbook: person id, date, and leave type, with approximately 8000 rows of data.

    I would like have a macro loop through all the dates and identify when the dates appear consecutively i.e. row 1: 2013-01-01, row 2: 2013-01-02 .. etc
    and in a new column insert the person id value, the start date, the end date, and the leave type for this range of dates.

    The following rules have to be followed by the macro:
    1. the person must be the same for each row (i.e. same integer in each cell)
    2. the leave type must be the same (i.e. same integer in each cell)
    3. Weekends need to be disregarded, (and in fact no dates that occur on weekends will appear in the data).

    Ive attached an example workbook with the before/ after examples - which perhaps will give you a better idea of whats going on.

    I had a decent crack at doing this today but after an hour or so of soul searching i decided that perhaps this could be stretching my (limited) vb programming skills.

    Kind Regards
    Jordan
    Attached Files Attached Files
    Last edited by jordan2322; 05-21-2013 at 10:11 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    Hi Jordan,

    Look at the Pivot Table in the attached. If you read it correctly you can get your information from it. Look down the columns at consecutive days and it will show groups of star and end dates. That is the best I could do without needing VBA. I hope it makes sense to you...
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    Thanks for the reply MP,

    I was wanting a vba macro to do this for me, as I need to be able to import the result into a database afterwards - obviously doing this by reading from a pivot table is not something i'd like to do :P.

    so far I have the following:

    Please Login or Register  to view this content.
    But it isnt quite working how it should, I've attached another workbook with the code in it, which also has a much larger dataset so you can see where its going wrong..

    Thanks again
    Attached Files Attached Files

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    OK Jordan,

    After a lot of trial and error try my code. Run it on Sheet1 and step through it to see if it does what you want. It looks like this:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    thanks for you quick reply MP,

    I ran it through and noticed that a couple of things happen incorrectly,

    it doesnt appear to be identifying the change in person,

    and it seems to output more rows than in the data - this should not happen as if we assume there are ranges of dates then multiple rows of the data would be reduced into a single row in the output.

    regards
    Jordan

  6. #6
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    Solved

    Thanks for your help MP!

    Please Login or Register  to view this content.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Splitting single column of dates to 2 columns representing a beginning and end date

    Hi Jordan,

    I agree on my code. It didn't work. I still think you are going to want the total times a person took leave and of what type. I'm still thinking a Pivot Table is what you want. See the attached which has a filter for each person. See if you really don't want these numbers.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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