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.

IDStatusFirstLastMarketStartEndDaysValue/DayTotal Value
ABCConfirmedABCXYZUK15/10/1920/10/195500025000
BCDConfirmedABCXYZUS28/10/1903/11/19610006000
CDEConfirmedABCXYZUK05/11/1907/11/1925001000

<tbody>
</tbody>

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):

IDStatusFirstLastMarketStartEndDaysValue/DayTotal Value
ABCConfirmedABCXYZUK15/10/1920/10/195500025000
BCDConfirmedABCXYZUS28/10/1901/11/19410004000
BCDConfirmedABCXYZUS01/11/1903/11/19210002000
CDEConfirmedABCXYZUK05/11/1907/11/192500
1000

<tbody>
</tbody>

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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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