Splitting Data into Months based on Date Range

markm94

New Member
Joined
Sep 2, 2019
Messages
4
I have been doing some research but was unable to find exactly what I am trying to achieve. I have been working with VBA frequently but am not quite advanced enough yet to do do this, so I would really appreciate some help!

I have a list of customer bookings, with various information and a date range. I need the customer bookings that overlap several months to be split into separate rows. Customer bookings that are entirely within a single month do not need to be split. The tricky part is that I need to have some of the fields calculated based on the date rage. For example, I need the number of days for each booking to be split into the corresponding months and the booking value to be split too.

[TABLE="width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Status[/TD]
[TD]First[/TD]
[TD]Last[/TD]
[TD]Market[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD]Days[/TD]
[TD]Value/Day[/TD]
[TD]Total Value[/TD]
[/TR]
[TR]
[TD]ABC[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]UK[/TD]
[TD]15/10/19[/TD]
[TD]20/10/19[/TD]
[TD]5[/TD]
[TD]5000[/TD]
[TD]25000[/TD]
[/TR]
[TR]
[TD]BCD[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]US[/TD]
[TD]28/10/19[/TD]
[TD]03/11/19[/TD]
[TD]6[/TD]
[TD]1000[/TD]
[TD]6000[/TD]
[/TR]
[TR]
[TD]CDE[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]UK[/TD]
[TD]05/11/19[/TD]
[TD]07/11/19[/TD]
[TD]2[/TD]
[TD]500[/TD]
[TD]1000[/TD]
[/TR]
</tbody>[/TABLE]

The above should convert into the below, splitting the second booking into 2 rows and calculating the Days and Total Value (days multiplied by Value/Day):

[TABLE="width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Status[/TD]
[TD]First[/TD]
[TD]Last[/TD]
[TD]Market[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD]Days[/TD]
[TD]Value/Day[/TD]
[TD]Total Value[/TD]
[/TR]
[TR]
[TD]ABC[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]UK[/TD]
[TD]15/10/19[/TD]
[TD]20/10/19[/TD]
[TD]5[/TD]
[TD]5000[/TD]
[TD]25000[/TD]
[/TR]
[TR]
[TD]BCD[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]US[/TD]
[TD]28/10/19[/TD]
[TD]01/11/19[/TD]
[TD]4[/TD]
[TD]1000[/TD]
[TD]4000[/TD]
[/TR]
[TR]
[TD]BCD[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]US[/TD]
[TD]01/11/19[/TD]
[TD]03/11/19[/TD]
[TD]2[/TD]
[TD]1000[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]CDE[/TD]
[TD]Confirmed[/TD]
[TD]ABC[/TD]
[TD]XYZ[/TD]
[TD]UK[/TD]
[TD]05/11/19[/TD]
[TD]07/11/19[/TD]
[TD]2[/TD]
[TD]500
[/TD]
[TD]1000[/TD]
[/TR]
</tbody>[/TABLE]

If there are bookings that are more long term, they would have to be split into several rows (for instance a booking from October to December will have to be split into 3).

Thank you for your help in advance!! :)
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Welcome to the Board!

Try this on a copy of your workbook:

Code:
Sub SplitBookings()
Dim r As Long

    Application.ScreenUpdating = False
    r = 2
    While Cells(r, "A") <> ""
        If Cells(r, "G") > WorksheetFunction.EoMonth(Cells(r, "F"), 0) + 1 Then
            Rows(r + 1).Insert
            Rows(r).Copy Rows(r + 1)
            Cells(r, "G") = WorksheetFunction.EoMonth(Cells(r, "F"), 0) + 1
            Cells(r + 1, "F") = Cells(r, "G")
            Cells(r, "H") = Cells(r, "G") - Cells(r, "F")
            Cells(r, "J") = Cells(r, "H") * Cells(r, "I")
            Cells(r + 1, "H") = Cells(r + 1, "G") - Cells(r + 1, "F")
            Cells(r + 1, "J") = Cells(r + 1, "H") * Cells(r + 1, "I")
        End If
        r = r + 1
    Wend
    Application.ScreenUpdating = True
            
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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