How to split and expand date and hours?

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
310
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone,

How to split and expand the date and time with a formula or VBA?

I have the "start date and time" that begins in one date and sometimes ends in next day. For this cases I'd like to split in two
rows from begin date until same date up to 23:59 and continue in next row the next date until end hour. For the cases where start
date and end date is the same only split in one row.

Below is the input 2 columns with blue headers and output I'd like to get in green headers.

Start Date & TimeEnd Date & TimeStart DateStart TimeEnd DateEnd Time
10/13/2022 23:4410/14/2022 2:202022-10-1323:442022-10-130:59
10/24/2022 23:1710/24/2022 23:572022-10-140:002022-10-142:20
2022-10-2423:172022-10-2423:51


Thanks in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Time.xlsm
ABCDEFGH
1info
2Start Date & TimeEnd Date & TimeStart DateStart TimeEnd DateEnd Time
313-Oct-22 23:4414-Oct-22 02:2002:3613-Oct-22 23:44:2000:00:0023:59:5900:15:39
424-Oct-22 23:1724-Oct-22 23:5700:4014-Oct-22 00:0014-Oct-22 02:20:2502:20:25
524-Oct-22 23:17:1924-Oct-22 23:57:4300:40:24
6
7
11a
Cell Formulas
RangeFormula
D3D3=INT(A3)
E3E3=MOD(A3,1)
H3:H5H3=G3-E3
C3:C4C3=B3-A3
G4:G5G4=MOD(B3,1)
E5E5=MOD(A4,1)
 
Upvote 0
Thanks Dave for your answer. I'd like to get a way to dynamically do the expand to 2 rows when needed.
In this sample only show 2 input rows, but actual data has several input dates and times.
 
Upvote 0
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
365 methods: Instead of absolute dell references, use DROP(FILTER(A:A,A:A<>""),1), or bring it in a =lambda helper like this
Excel Formula:
=DROP(REDUCE(0,$A3:INDEX($A3:$A5001,COUNTA($A3:$A5001)),LAMBDA(a,b,VSTACK(a,INT(b)))),1)
 
Upvote 0
ABCDEFGH
Start Date & TimeEnd Date & TimeStart DateStart TimeEnd DateEnd Time
13-10-2022 23:44​
15-10-2022 02:20​
13-10-2022
23:44:00​
13-10-2022
23:59:59​
24-10-2022 23:17​
24-10-2022 23:57​
14-10-2022
00:00:00​
14-10-2022
23:59:59​
15-10-2022
00:00:00​
15-10-2022
02:20:00​
24-10-2022
23:17:00​
24-10-2022
23:57:00​

Code for macro
VBA Code:
Sub Rearrange_Dates()
Dim A, K
Dim T As Long, Ta As Long, dys As Long, X As Long
A = Range("B3").CurrentRegion

With CreateObject("SCripting.Dictionary")
For T = 2 To UBound(A, 1)
dys = Int(A(T, 2)) - Int(A(T, 1))
If dys = 0 Then
X = X + 1: .Add X, Array(Int(A(T, 1)), A(T, 1) - Int(A(T, 1)), Int(A(T, 2)), A(T, 2) - Int(A(T, 2)))
Else
    For Ta = 0 To dys
    X = X + 1
    ReDim ary(0 To 3)
    If Ta = 0 Then
    ary(0) = Int(A(T, 1)): ary(1) = A(T, 1) - Int(A(T, 1)): ary(2) = Int(A(T, 1)): ary(3) = TimeValue("23:59:59")
    ElseIf Ta < dys Then
    ary(0) = Int(A(T, 1)) + Ta: ary(1) = TimeValue("0:00:00"): ary(2) = Int(A(T, 1)) + Ta: ary(3) = TimeValue("23:59:59")
    Else
    ary(0) = Int(A(T, 1)) + Ta: ary(1) = TimeValue("0:00:00"): ary(2) = Int(A(T, 1)) + Ta: ary(3) = A(T, 2) - Int(A(T, 2))
    End If
        
    .Add X, ary
    Next Ta
End If
Next T
K = .items
End With

With Range("E3")
.CurrentRegion.Offset(1, 0).ClearComments
.Offset(1, 0).Resize(X, 4) = WorksheetFunction.Index(K, 0, 0)
.CurrentRegion.Offset(1, 0).Columns(2).NumberFormat = "hh:mm:ss"
.CurrentRegion.Offset(1, 0).Columns(4).NumberFormat = "hh:mm:ss"
End With
End Sub
 
Upvote 0
a relatively simple alternative
Time.xlsm
ABDEF
10Start Date & TimeEnd Date & TimeTime Start DayTime Next dayTotal
1113-Oct-22 23:4414-Oct-22 02:2000:1502:2002:36
1224-Oct-22 23:1724-Oct-22 23:5700:4000:0000:40
13
11a
Cell Formulas
RangeFormula
D11:D12D11=IF(INT(B11)>INT(A11),INT(B11)-A11,B11-A11)
E11:E12E11=IF(INT(B11)>INT(A11),MOD(B11,1),0)
F11:F12F11=D11+E11
 
Upvote 0
ABCDEFGH
Start Date & TimeEnd Date & TimeStart DateStart TimeEnd DateEnd Time
13-10-2022 23:44​
15-10-2022 02:20​
13-10-2022
23:44:00​
13-10-2022
23:59:59​
24-10-2022 23:17​
24-10-2022 23:57​
14-10-2022
00:00:00​
14-10-2022
23:59:59​
15-10-2022
00:00:00​
15-10-2022
02:20:00​
24-10-2022
23:17:00​
24-10-2022
23:57:00​

Code for macro
VBA Code:
Sub Rearrange_Dates()
Dim A, K
Dim T As Long, Ta As Long, dys As Long, X As Long
A = Range("B3").CurrentRegion

With CreateObject("SCripting.Dictionary")
For T = 2 To UBound(A, 1)
dys = Int(A(T, 2)) - Int(A(T, 1))
If dys = 0 Then
X = X + 1: .Add X, Array(Int(A(T, 1)), A(T, 1) - Int(A(T, 1)), Int(A(T, 2)), A(T, 2) - Int(A(T, 2)))
Else
    For Ta = 0 To dys
    X = X + 1
    ReDim ary(0 To 3)
    If Ta = 0 Then
    ary(0) = Int(A(T, 1)): ary(1) = A(T, 1) - Int(A(T, 1)): ary(2) = Int(A(T, 1)): ary(3) = TimeValue("23:59:59")
    ElseIf Ta < dys Then
    ary(0) = Int(A(T, 1)) + Ta: ary(1) = TimeValue("0:00:00"): ary(2) = Int(A(T, 1)) + Ta: ary(3) = TimeValue("23:59:59")
    Else
    ary(0) = Int(A(T, 1)) + Ta: ary(1) = TimeValue("0:00:00"): ary(2) = Int(A(T, 1)) + Ta: ary(3) = A(T, 2) - Int(A(T, 2))
    End If
       
    .Add X, ary
    Next Ta
End If
Next T
K = .items
End With

With Range("E3")
.CurrentRegion.Offset(1, 0).ClearComments
.Offset(1, 0).Resize(X, 4) = WorksheetFunction.Index(K, 0, 0)
.CurrentRegion.Offset(1, 0).Columns(2).NumberFormat = "hh:mm:ss"
.CurrentRegion.Offset(1, 0).Columns(4).NumberFormat = "hh:mm:ss"
End With
End Sub
Thanks for your answer. not sure why but I getting run time '13' type mismatch in this line "dys = Int(A(T, 2)) - Int(A(T, 1))".
 
Upvote 0

Forum statistics

Threads
1,214,629
Messages
6,120,630
Members
448,973
Latest member
ChristineC

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