Comparing times with different text formats

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I am trying to compare two lots of times and got as far as the code below. In Cells A1, A2 and A3 I have three times in the format +02'00... and in Cells B1, B2, B3 I have three times in the format 00:17:00. If the time in Column A are greater than the times in Column B I want a Yes in Columns C. but I keep seeing the times as 1.18055 for example.

Code:
Sub testing()
Dim cell As Range
    For Each cell In Range(Cells(1, 1), Cells(3, 1))
        If cell.Value <> "" Then
            If Mid(cell.Offset(0, 0).Value, 2, 2) * 60 + Mid(cell.Offset(0, 0).Value, 5, 2) >= Format(Mid(cell.Offset(0, 1).Value, 4, 2) * 60 _
                + Mid(cell.Offset(0, 1).Value, 7, 2), "hh:mm:ss") Then
                cell.Offset(0, 3).Value = "Yes"
                Else
                cell.Offset(0, 3).Value = "No"
            End If
        End If
    Next cell
End Sub

+02'00

<colgroup><col style="width:48pt" width="64" span="4"> </colgroup><tbody>
</tbody>
00:17:00

<tbody>
</tbody>
No
+03'00
00:01:00
Yes
+00'30
00:02:00
No

<tbody>
</tbody>







Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How about
Code:
Sub testing()
Dim cell As Range
    For Each cell In Range(Cells(1, 1), Cells(3, 1))
        If cell.Value <> "" Then
            If Mid(cell.Offset(0, 0).Value, 2, 2) * 1 + (Mid(cell.Offset(0, 0).Value, 5, 2) / 60) >= _
               Minute(cell.Offset(0, 1).Value) + (Second(cell.Offset(, 1)) / 60) Then
                cell.Offset(0, 3).Value = "Yes"
                Else
                cell.Offset(0, 3).Value = "No"
            End If
        End If
    Next cell
End Sub
 
Upvote 0
That works a treat thanks :)

One little bit extra. I can take one from the other and get the results too as well now thanks. But if I wanted the result in the format of 00:15:00 instead of 15?

Thanks
 
Upvote 0
What do you mean by "the result", it's only putting either Yes or No into the sheet.
 
Upvote 0
I just thought of putting the resulting time difference on Column D. Which works well using your formulas.
Code:
cell.Offset(0, 4).Value = Mid(cell.Offset(0, 0).Value, 2, 2) * 1 + (Mid(cell.Offset(0, 0).Value, 5, 2) / 60) - Minute(cell.Offset(0, 1).Value) + (Second(cell.Offset(, 1)) / 60)

But instead of the result saying 15 I wondered how it could be displayed in the format hh:mm:ss i.e. 00:15:00.
More of a side question really.
 
Upvote 0
Try
Code:
Sub testing()
   Dim x, y
   Dim cell As Range
   For Each cell In Range(Cells(1, 1), Cells(3, 1))
      If cell.Value <> "" Then
         x = (Mid(cell.Offset(0, 0).Value, 2, 2) / 60) + (Mid(cell.Offset(0, 0).Value, 5, 2) / 3600)
         y = cell.Offset(, 1).Value * 24
         If x >= y Then
            cell.Offset(0, 3).Value = "Yes"
         Else
            cell.Offset(0, 3).Value = "No"
         End If
         cell.Offset(, 4).Value = Format((x - y) / 24, "hh:mm:ss")
      End If
   Next cell
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,060
Latest member
mtsheetz

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