VBA separate range of dates in cells with duration

tequilasunsette

New Member
Joined
Nov 24, 2014
Messages
1
I need to split dates along with their respective duration.

example:
Begin Date | End Date |Duration
3-Nov 5-Nov 2.5


**I need it to look like this in excel:**
3-Nov | 1
4-Nov | 1
5-Nov | 0.5

I need the half day to be shown on the last day. I am not sure how to make this split.
Appreciate the help this forum provides. This will really save my life!!!


this code works only with splitting the dates. dont know how to apply that and split the days as well :(


Code:
Sub SeperateDateRange()
    Dim Ws As Worksheet
    Dim nCol As Integer

    'Define sheet
    Set Ws = ActiveSheet

    nCol = 1 '<~~ Defines the number of columns before the date columns

    Application.ScreenUpdating = False

    'Loops throuh cells
    For i = 1 To ActiveSheet.Cells(Rows.Count, nCol + 2).End(xlUp).Row - 1 Step 1
        For j = 0 To Ws.Cells(i + 1, nCol + 2).Value - Ws.Cells(i + 1, nCol + 1).Value Step 1

            With Ws.Cells(Ws.Cells(Rows.Count, 1).End(xlUp).Row + 1, 1)
                For k = 0 To nCol - 1 Step 1
                    .Offset(0, k).Value = Ws.Cells(i + 1, k + 1).Value
                Next k
                .Offset(0, nCol).Value = DateSerial(Year(Ws.Cells(i + 1, nCol + 1).Value), Month(Ws.Cells(i + 1, nCol + 1).Value), Day(Ws.Cells(i + 1, nCol + 1).Value) + j)
            End With
        Next j
    Next i

    Application.ScreenUpdating = True
End Sub


thanks for your help!

regards,
Marvin.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hello,

I assume the duration is almost equal to the number of days between the two dates.

Code:
Sub SPLIT_DATES()
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        MY_START_DATE = Range("A" & MY_ROWS).Value
        MY_END_DATE = Range("B" & MY_ROWS).Value
        MY_DURATION = Range("C" & MY_ROWS).Value
        MY_DATES = MY_END_DATE + 1 - MY_START_DATE
        MY_DURATION_SPLIT = MY_DURATION / MY_DATES
        For MY_DATE_ROWS = 0 To MY_DATES - 2
            Range("E" & Rows.Count).End(xlUp).Offset(1, 0).Value = MY_START_DATE + MY_DATE_ROWS
            Range("F" & Rows.Count).End(xlUp).Offset(1, 0).Value = Round(MY_DURATION_SPLIT, 0)
            MY_COUNT = MY_COUNT + Round(MY_DURATION_SPLIT, 0)
        Next MY_DATE_ROWS
            Range("E" & Rows.Count).End(xlUp).Offset(1, 0).Value = MY_END_DATE
            Range("F" & Rows.Count).End(xlUp).Offset(1, 0).Value = MY_DURATION - MY_COUNT
            MY_COUNT = 0
    Next MY_ROWS
End Sub

This will put the data in cols E and F. Not sure how it will work on other data.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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