Sorting spreadsheet by time

systemdude

New Member
Joined
Apr 9, 2015
Messages
6
Hi

I have a spreadsheet that contains among other columns, 2 columns. They are start time and end time. Their format is 7/1/2019 2:17:11 AM for example. Of all the start times, I want to ONLY retain the information where the start time is */*/2019 21:00:00 PM and the end time is */*/2019 22:00:00 PM.

How would I go about doing this?

Many thanks.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello,

Your description is not totally clear.

My understanding is that you want to delete the rows where the start time isn't 21:00 in 2019 and the end time isn't 22:00 in 2019.

Code:
Sub keep_2019_2100_2200()
    For MY_ROWS = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Year(Range("A" & MY_ROWS).Value) <> 2019 Or Hour(Range("A" & MY_ROWS)) <> 21 Or _
        Year(Range("B" & MY_ROWS).Value) <> 2019 Or Hour(Range("B" & MY_ROWS)) <> 22 Then
            Rows(MY_ROWS).Delete
        End If
    Next MY_ROWS
End Sub
 
Upvote 0
Hello and many thanks. You are right, I was not clear. In addition to how you have interpreted it, I also want to retain rows where the start and end times fall in the same hour, that is, in 2100 hours or 2200 hours. So I tried to modify your code as follows - is this right?


Sub keep_2019_2100_2200()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For MY_ROWS = Range("F" & Rows.Count).End(xlUp).Row To 2 Step -1
If Year(Range("F" & MY_ROWS).Value) <> 2019 Or Hour(Range("F" & MY_ROWS)) <> 21 Or Hour(Range("F" & MY_ROWS)) <> 22 Or _
Hour(Range("G" & MY_ROWS)) <> 21 Or Hour(Range("G" & MY_ROWS)) <> 22 Or _
Year(Range("G" & MY_ROWS).Value) <> 2019 Then
Rows(MY_ROWS).Delete
End If
Next MY_ROWS
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub




==========================================
Your description is not totally clear.

My understanding is that you want to delete the rows where the start time isn't 21:00 in 2019 and the end time isn't 22:00 in 2019.

Code:
Sub keep_2019_2100_2200()
    For MY_ROWS = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Year(Range("A" & MY_ROWS).Value) <> 2019 Or Hour(Range("A" & MY_ROWS)) <> 21 Or _
        Year(Range("B" & MY_ROWS).Value) <> 2019 Or Hour(Range("B" & MY_ROWS)) <> 22 Then
            Rows(MY_ROWS).Delete
        End If
    Next MY_ROWS
End Sub
[/QUOTE]
 
Upvote 0
Hello,

doesn't my original code not do that?

If i start with

01/04/2019 20:00 01/04/2019 21:00
01/04/2019 21:00 01/04/2019 22:00
01/04/2019 21:01 01/04/2019 22:01
01/04/2019 21:02 01/04/2019 22:02
01/04/2019 21:03 01/04/2019 22:03
01/04/2019 21:04 01/04/2019 22:04
01/04/2019 21:05 01/04/2019 22:05
01/04/2019 21:06 01/04/2019 22:06
01/04/2019 21:07 01/04/2019 22:07
01/04/2019 21:08 01/04/2019 22:08
01/04/2019 22:00 01/04/2019 23:00

then run the code, I end up with

01/04/2019 21:00 01/04/2019 22:00
01/04/2019 21:01 01/04/2019 22:01
01/04/2019 21:02 01/04/2019 22:02
01/04/2019 21:03 01/04/2019 22:03
01/04/2019 21:04 01/04/2019 22:04
01/04/2019 21:05 01/04/2019 22:05
01/04/2019 21:06 01/04/2019 22:06
01/04/2019 21:07 01/04/2019 22:07
01/04/2019 21:08 01/04/2019 22:08

perhaps you can post a sample of data, with a before and after.
 
Upvote 0
Before:

GATNUNKRadarsat-260272X7/1/2019 20:457/1/2019 20:53SuccessUNKfinal
ICANUNKRadarsat-260272X7/1/2019 20:567/1/2019 21:04SuccessUNKfinal
GATNUNKRCM-2288S7/1/2019 21:377/1/2019 21:47Waiting for ReportUNKfinal
PASSUNKRadarsat-260273X7/1/2019 22:277/1/2019 22:38SuccessUNKfinal
ICANUNKRadarsat-260273X7/1/2019 22:387/1/2019 22:41SuccessUNKfinal
PASSUNKRCM-3289S7/1/2019 23:157/1/2019 23:25Waiting for ReportUNKfinal
PASSUNKRadarsat-260274X7/2/2019 0:057/2/2019 0:16SuccessUNKfinal
ICANUNKRadarsat-260274X7/2/2019 0:197/2/2019 0:22SuccessUNKfinal

<colgroup><col><col span="2"><col><col><col span="2"><col><col><col></colgroup><tbody>
</tbody>


After
GATNUNKRadarsat-260272X7/1/2019 20:457/1/2019 20:53SuccessUNKfinal

<tbody>
</tbody>
Here is the exact code I ran, the times are in columns F and G:

Sub keep_2019_2100_2200()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For MY_ROWS = Range("F" & Rows.Count).End(xlUp).Row To 2 Step -1
If Year(Range("F" & MY_ROWS).Value) <> 2019 Or Hour(Range("F" & MY_ROWS)) <> 21 Or _
Year(Range("G" & MY_ROWS).Value) <> 2019 Or Hour(Range("G" & MY_ROWS)) <> 22 Then
Rows(MY_ROWS).Delete
End If
Next MY_ROWS
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub








Hello,

doesn't my original code not do that?

If i start with

01/04/2019 20:00 01/04/2019 21:00
01/04/2019 21:00 01/04/2019 22:00
01/04/2019 21:01 01/04/2019 22:01
01/04/2019 21:02 01/04/2019 22:02
01/04/2019 21:03 01/04/2019 22:03
01/04/2019 21:04 01/04/2019 22:04
01/04/2019 21:05 01/04/2019 22:05
01/04/2019 21:06 01/04/2019 22:06
01/04/2019 21:07 01/04/2019 22:07
01/04/2019 21:08 01/04/2019 22:08
01/04/2019 22:00 01/04/2019 23:00

then run the code, I end up with

01/04/2019 21:00 01/04/2019 22:00
01/04/2019 21:01 01/04/2019 22:01
01/04/2019 21:02 01/04/2019 22:02
01/04/2019 21:03 01/04/2019 22:03
01/04/2019 21:04 01/04/2019 22:04
01/04/2019 21:05 01/04/2019 22:05
01/04/2019 21:06 01/04/2019 22:06
01/04/2019 21:07 01/04/2019 22:07
01/04/2019 21:08 01/04/2019 22:08

perhaps you can post a sample of data, with a before and after.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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