Need to split a data range into one row per day [VBA]

Mattyastill

New Member
Joined
Nov 27, 2017
Messages
23
Hi Everyone,

Looking for some assitance with a VBA macro that would split a date range into seperate rows for each day within the range.

I have several thousand rows of data which are holiday requests and the data consists of 9 rows (see image)

1629379913872.png


I need the new list to start in "Sheet 2" in cell "A1" where each day within the date range is an individual row. Obviously the new data would only contain "Date" instead of "Start Date" & "End Date"

Appreciate any help on this.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
And which data should be copied in which column? Or you only need a sequence of dates created from each of the Start/EndDate? All the dates need to be listed, eg 12 lines in case that Start is Aug 16 and End is Aug 27?
Bye
 
Upvote 0
And which data should be copied in which column? Or you only need a sequence of dates created from each of the Start/EndDate? All the dates need to be listed, eg 12 lines in case that Start is Aug 16 and End is Aug 27?
Bye
For each employee ID i would need the number of rows that exist in the range Start Date: End Date.

For example Employe ID: 1000. Would have 3 rows where it would look like the below.

1629384625098.png
 
Upvote 0
Try the following macro:
VBA Code:
Sub Expand()
Dim I As Long, DeSh As Worksheet, J As Long
Dim jInd As Long
'
jInd = 1
Set DeSh = Sheets("Sheet2")             '<<< The destination Sheet
DeSh.Range("A:I").ClearContents
Cells(1, 1).Resize(1, 9).Copy DeSh.Range("A1")
For I = 2 To Cells(Rows.Count, "F").End(xlUp).Row
    For J = Cells(I, "E").Value To Cells(I, "F").Value
        jInd = jInd + 1
        DeSh.Cells(jInd, 1).Resize(1, 9).Value = Cells(I, 1).Resize(1, 9).Value
        DeSh.Cells(jInd, "E").Resize(1, 2).Value = J
    Next J
Next I
End Sub
Copy the code to a Standard Module of your vba-project; then select the worksheet with the starting datas and start Sub Expand

Note that Columns A:I of the destination sheet will be CLEARED WITHOUT ANY ADVICE before creating the new table

Bye
 
Upvote 0
Another way using Power Query.

ExpandIP.xlsm
ABCDEFGHIJKLMNOPQRS
1Emp IdFirstLastLocationStartEndDurationStatusApprovedEmp IdFirstLastLocationStart DateEnd DateDurationStatusApproved
21000abUK8/16/20218/18/202122:30Approvedx1000abUK8/16/20218/16/202122:30Approvedx
31001cdUK8/16/20218/18/202115:00Approvedx1000abUK8/17/20218/17/202122:30Approvedx
41002efUK7/12/20217/15/202122:30Approvedx1000abUK8/18/20218/18/202122:30Approvedx
51003ghUK7/16/20217/18/202115:00Approvedx1001cdUK8/16/20218/16/202115:00Approvedx
61004ijUK8/1/20218/3/202122:30Approvedx1001cdUK8/17/20218/17/202115:00Approvedx
71001cdUK8/18/20218/18/202115:00Approvedx
81002efUK7/12/20217/12/202122:30Approvedx
91002efUK7/13/20217/13/202122:30Approvedx
101002efUK7/14/20217/14/202122:30Approvedx
111002efUK7/15/20217/15/202122:30Approvedx
121003ghUK7/16/20217/16/202115:00Approvedx
131003ghUK7/17/20217/17/202115:00Approvedx
141003ghUK7/18/20217/18/202115:00Approvedx
151004ijUK8/1/20218/1/202122:30Approvedx
161004ijUK8/2/20218/2/202122:30Approvedx
171004ijUK8/3/20218/3/202122:30Approvedx
Sheet9


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    toDate = Table.TransformColumnTypes(Source,{{"Start", type date}, {"End", type date}}),
    List = Table.AddColumn(toDate, "Start Date", each List.Dates([Start],Number.From([End]-[Start])+1,#duration(1,0,0,0))),
    Expand = Table.ExpandListColumn(List, "Start Date"),
    Dupe = Table.DuplicateColumn(Expand, "Start Date", "End Date"),
    RC = Table.RemoveColumns(Dupe,{"Start", "End"}),
    Reorder = Table.ReorderColumns(RC,{"Emp Id", "First", "Last", "Location", "Start Date", "End Date", "Duration", "Status", "Approved"}),
    Types = Table.TransformColumnTypes(Reorder,{{"Start Date", type date}, {"End Date", type date}, {"First", type text}, {"Last", type text}, {"Location", type text}, {"Emp Id", Int64.Type}, {"Duration", type time}, {"Status", type text}, {"Approved", type text}})
in
    Types
 
Upvote 0
Try the following macro:
VBA Code:
Sub Expand()
Dim I As Long, DeSh As Worksheet, J As Long
Dim jInd As Long
'
jInd = 1
Set DeSh = Sheets("Sheet2")             '<<< The destination Sheet
DeSh.Range("A:I").ClearContents
Cells(1, 1).Resize(1, 9).Copy DeSh.Range("A1")
For I = 2 To Cells(Rows.Count, "F").End(xlUp).Row
    For J = Cells(I, "E").Value To Cells(I, "F").Value
        jInd = jInd + 1
        DeSh.Cells(jInd, 1).Resize(1, 9).Value = Cells(I, 1).Resize(1, 9).Value
        DeSh.Cells(jInd, "E").Resize(1, 2).Value = J
    Next J
Next I
End Sub
Copy the code to a Standard Module of your vba-project; then select the worksheet with the starting datas and start Sub Expand

Note that Columns A:I of the destination sheet will be CLEARED WITHOUT ANY ADVICE before creating the new table

Bye
That works perfectly! Thank you.
 
Upvote 0
Another way using Power Query.

ExpandIP.xlsm
ABCDEFGHIJKLMNOPQRS
1Emp IdFirstLastLocationStartEndDurationStatusApprovedEmp IdFirstLastLocationStart DateEnd DateDurationStatusApproved
21000abUK8/16/20218/18/202122:30Approvedx1000abUK8/16/20218/16/202122:30Approvedx
31001cdUK8/16/20218/18/202115:00Approvedx1000abUK8/17/20218/17/202122:30Approvedx
41002efUK7/12/20217/15/202122:30Approvedx1000abUK8/18/20218/18/202122:30Approvedx
51003ghUK7/16/20217/18/202115:00Approvedx1001cdUK8/16/20218/16/202115:00Approvedx
61004ijUK8/1/20218/3/202122:30Approvedx1001cdUK8/17/20218/17/202115:00Approvedx
71001cdUK8/18/20218/18/202115:00Approvedx
81002efUK7/12/20217/12/202122:30Approvedx
91002efUK7/13/20217/13/202122:30Approvedx
101002efUK7/14/20217/14/202122:30Approvedx
111002efUK7/15/20217/15/202122:30Approvedx
121003ghUK7/16/20217/16/202115:00Approvedx
131003ghUK7/17/20217/17/202115:00Approvedx
141003ghUK7/18/20217/18/202115:00Approvedx
151004ijUK8/1/20218/1/202122:30Approvedx
161004ijUK8/2/20218/2/202122:30Approvedx
171004ijUK8/3/20218/3/202122:30Approvedx
Sheet9


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    toDate = Table.TransformColumnTypes(Source,{{"Start", type date}, {"End", type date}}),
    List = Table.AddColumn(toDate, "Start Date", each List.Dates([Start],Number.From([End]-[Start])+1,#duration(1,0,0,0))),
    Expand = Table.ExpandListColumn(List, "Start Date"),
    Dupe = Table.DuplicateColumn(Expand, "Start Date", "End Date"),
    RC = Table.RemoveColumns(Dupe,{"Start", "End"}),
    Reorder = Table.ReorderColumns(RC,{"Emp Id", "First", "Last", "Location", "Start Date", "End Date", "Duration", "Status", "Approved"}),
    Types = Table.TransformColumnTypes(Reorder,{{"Start Date", type date}, {"End Date", type date}, {"First", type text}, {"Last", type text}, {"Location", type text}, {"Emp Id", Int64.Type}, {"Duration", type time}, {"Status", type text}, {"Approved", type text}})
in
    Types
I never thought about using power query but i will explore this as an option and see which is a better fit. Thank you!
 
Upvote 0
For each employee ID i would need the number of rows that exist in the range Start Date: End Date.

For example Employe ID: 1000. Would have 3 rows where it would look like the below.

View attachment 45212
Hi, I used this code for a similar query with success, I just have problem if the Start date and end date are the same the macro instead of giving me one line report 366 (the day + 365 additional days like following the whole year).

How can correct that?

Thank you in advanced guys.
 
Upvote 0
Are you using my Sub Expand?

I tested the code and I had not the problem you report

Are you sure that columns E:F contains real "dates" (not strings); can you show your set or data (at least a picture)?
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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