Sub DateTimeSplit()
'Define Variables
'----------------
Dim StDate As Variant 'Start Date - for copying range and date checks
Dim StTime As Variant 'Start Time - for copying range and time checks
Dim EndDate As Variant 'End Date - for copying range and date checks
Dim EndTime As Variant 'End Time - for copying range and time checks
Dim MTH As Integer 'Month - for date checks
Dim StRow As Variant 'Start Row - for copying range
Dim LstRow As Variant 'End Row - for copying range
Dim Rng As Range 'for copying range
Dim CurrWB As String 'File to copy from
Dim NewWB As String 'File to paste to
Dim Chk As Integer 'check if time needed by user
CurrWB = ActiveWorkbook.Name
If MsgBox("Do you also want to check time?", vbYesNo, "") = vbNo Then Chk = 1 Else Chk = 2
'Set Start Date
'--------------
StDate = Application.InputBox("Date Format YYYY/MM/DD e.g. 2018/01/30", "Start Date")
If StDate = False Then
MsgBox "Start Date Cancelled" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf StDate = "" Then
MsgBox "Start Date is empty" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf Not StDate Like "####/##/##" Then
MsgBox "Start Date is in incorrect format" & vbNewLine & "Must be YYYY/MM/DD" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
Else
MTH = Split(StDate, "/")(1)
If MTH >= 12 Then
MsgBox "Start Date is in incorrect format" & vbNewLine & "Must be YYYY/MM/DD" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
End If
StDate = CDate(StDate)
End If
'Set Start Time
'--------------
If Chk = 2 Then
StTime = Application.InputBox("Date Format HH:MM 24 hour format e.g. 17:30", "Start Time")
If StTime = False Then
MsgBox "Start Time Cancelled" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf StTime = "" Then
MsgBox "Start Time is empty" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf Not StTime Like "##:##" Then
MsgBox "Start Time is in incorrect format" & vbNewLine & "Must be HH:MM 24 hour format" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
Else
StTime = CDate(StTime)
End If
End If
'Set End Date
'-------------
EndDate = Application.InputBox("Date Format YYYY/MM/DD e.g. 2018/01/30", "End Date")
If EndDate = False Then
MsgBox "End Date Cancelled" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf EndDate = "" Then
MsgBox "End Date is empty" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf Not EndDate Like "####/##/##" Then
MsgBox "End Date is in incorrect format" & vbNewLine & "Must be YYYY/MM/DD" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
Else
MTH = Split(EndDate, "/")(1)
If MTH >= 12 Then
MsgBox "End Date is in incorrect format" & vbNewLine & "Must be YYYY/MM/DD" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
End If
EndDate = CDate(EndDate)
End If
'Set End Time
'------------
If Chk = 2 Then
EndTime = Application.InputBox("Date Format HH:MM 24 hour format e.g. 17:30", "End Time")
If EndTime = False Then
MsgBox "End Time Cancelled" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf EndTime = "" Then
MsgBox "End Time is empty" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
ElseIf Not EndTime Like "##:##" Then
MsgBox "End Time is in incorrect format" & vbNewLine & "Must be HH:MM 24 hour format" & vbNewLine & "Macro will now exit", vbCritical, ""
Exit Sub
Else
EndTime = CDate(EndTime)
End If
End If
'Combine Date & Time
'-------------------
If Chk = 2 Then StDate = StDate + StTime
If Chk = 2 Then EndDate = EndDate + EndTime
'Add Date + Time conversion formulae
'-----------------------------------
LstRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("E2:E" & LstRow).Formula = "=IFERROR(DATE(LEFT(A2,4),MID(A2,6,2),MID(A2,9,2)),E1)" 'Split Date from column A
Range("F2:F" & LstRow).Formula = "=IFERROR(MID(A2,12,5)*1,F1)" 'Split Time from column A
Range("G2:G" & LstRow).Formula = "=E2+F2" 'Date + Time
Range("E2:G" & LstRow).Copy
Range("E2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Paste Criteria
'---------------
Range("J2") = StDate
Range("J3") = EndDate
'Paste Range check formula
'-------------------------
If Chk = 2 Then
Range("H2:H" & LstRow).Formula = "=IF(AND(G2>=$J$2,G2<=$J$3),""A"",""B"")"
Else
Range("H2:H" & LstRow).Formula = "=IF(AND(E2>=$J$2,E2<=$J$3),""A"",""B"")"
End If
Range("H2:H" & LstRow).Copy
Range("H2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Range("K2").Formula = "=MATCH(""A"",H:H,0)"
Range("K3").Formula = "=MATCH(""A"",H:H,1)"
'Set Range
'---------
StRow = Range("K2")
LstRow = Range("K3")
Range("E:K").Delete
Set Rng = Range("A" & StRow & ":C" & LstRow)
'Paste to different file
'------------------------
Range("A1:C1").Copy
Workbooks.Add
NewWB = ActiveWorkbook.Name
Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
Windows(CurrWB).Activate
Rng.Copy
Windows(NewWB).Activate
Range("A2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Finish
'------
MsgBox "Macro completed", vbInformation, ""
End Sub