Consolidate data set

joelui

New Member
Joined
Nov 3, 2011
Messages
25
hi guys,

I have a set of data with 1 row for each date/staff/event, is it possible to consolidate them so that it shows the start and end date if the staff number and the code are the same? if the dates break the sequence then I would like it to list as a separate row?

thanks,
Joe

e.g. Current data set
Staff NumberStart dateEnd dateCode
2198-Jan-188-Jan-18Leave
2199-Jan-189-Jan-18Leave
21910-Jan-1810-Jan-18Leave
21911-Jan-1811-Jan-18Leave
21912-Jan-1812-Jan-18Leave
21913-Jan-1813-Jan-18Leave
21914-Jan-1814-Jan-18Leave
21915-Jan-1815-Jan-18Leave
21916-Jan-1816-Jan-18Leave
21917-Jan-1817-Jan-18Leave
21918-Jan-1818-Jan-18Leave
21919-Jan-1819-Jan-18Leave
21920-Jan-1820-Jan-18Leave
21921-Jan-1821-Jan-18Leave
21912-Feb-184-Mar-18Leave
2278-Jan-188-Jan-18OFFLN
2279-Jan-189-Jan-18OFFLN
2328-Jan-188-Jan-18Leave
2329-Jan-189-Jan-18Leave
23210-Jan-1810-Jan-18Leave
23211-Jan-1811-Jan-18Leave
23212-Jan-1812-Jan-18OFFLN
23213-Jan-1813-Jan-18OFFLN
23214-Jan-1814-Jan-18OFFLN

<tbody>
</tbody><colgroup><col><col><col span="2"></colgroup>


Consolidated data set:
Staff NumberStart dateEnd dateCode
2198-Jan-1821-Jan-18Leave
21912-Feb-184-Mar-18Leave
2278-Jan-189-Jan-18OFFLN
2328-Jan-1811-Jan-18Leave
23212-Jan-1814-Jan-18OFFLN

<tbody>
</tbody><colgroup><col><col><col span="2"></colgroup>
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this for results on sheet2.
Code:
[COLOR=navy]Sub[/COLOR] MG28Dec49
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] txt [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] q [COLOR=navy]As[/COLOR] Variant, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
ReDim Ray(1 To Rng.Count, 1 To 4)
[COLOR=navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    txt = Dn.Value & "," & Dn.Offset(, 3).Value
    [COLOR=navy]If[/COLOR] Not .Exists(txt) [COLOR=navy]Then[/COLOR]
       ReDim nRay(1 To Rng.Count, 1 To 2)
        nRay(1, 1) = CDate(Dn.Offset(, 1).Value): nRay(1, 2) = CDate(Dn.Offset(, 2).Value)
        .Add txt, Array(nRay, 1)
    [COLOR=navy]Else[/COLOR]
        q = .Item(txt)
        q(1) = q(1) + 1
        q(0)(q(1), 1) = CDate(Dn.Offset(, 1).Value)
        q(0)(q(1), 2) = CDate(Dn.Offset(, 2).Value)
        .Item(txt) = q
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR]
[COLOR=navy]Dim[/COLOR] K [COLOR=navy]As[/COLOR] Variant, Dt [COLOR=navy]As[/COLOR] Date, temp [COLOR=navy]As[/COLOR] Date
c = 1
Ray(1, 1) = "Staff Number": Ray(1, 2) = "Start date": Ray(1, 3) = "End date": Ray(1, 4) = "Code"
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] .keys
c = c + 1
    [COLOR=navy]For[/COLOR] n = 1 To .Item(K)(1)
        [COLOR=navy]For[/COLOR] Dt = CDate(.Item(K)(0)(n, 1)) To CDate(.Item(K)(0)(n, 2))
            [COLOR=navy]If[/COLOR] Ray(c, 2) = "" [COLOR=navy]Then[/COLOR]
                Ray(c, 1) = Split(K, ",")(0)
                Ray(c, 2) = Format(Dt, "dd-mmm-yy")
            [COLOR=navy]ElseIf[/COLOR] Not DateAdd("d", 1, temp) = Dt [COLOR=navy]Then[/COLOR]
                c = c + 1
                Ray(c, 1) = Split(K, ",")(0)
                Ray(c, 2) = Format(Dt, "dd-mmm-yy") 
                Ray(c, 3) = Format(Dt, "dd-mmm-yy")
                Ray(c, 4) = Split(K, ",")(1)
            [COLOR=navy]Else[/COLOR]
                Ray(c, 3) = Format(Dt, "dd-mmm-yy")
                Ray(c, 4) = Split(K, ",")(1)
            [COLOR=navy]End[/COLOR] If
            temp = Dt
        [COLOR=navy]Next[/COLOR] Dt
    [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] K
[COLOR=navy]End[/COLOR] With
[COLOR=navy]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 4)
    .Value = Ray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,155
Members
449,208
Latest member
emmac

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