Negative time differences

JonRowland

Active Member
Joined
May 9, 2003
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hi,

Sorry this appears to be have been spoken numberous times but I can't seem to find an answer that fits my problem.

I have two cells which contain date/time

L2 = 25/12/2010 12:00:01
M2 = 25/12/2010 13:00:01

I need to work out the difference between M2 and L2

so my vba for this is
<code>
Cells(Row, 15).Value = Cells(Row, 13) - Cells(Row, 12)
</code>

This is ok is time is positive for get ######### when negative. So how can I get it to show the negative as -1 ?

Jon
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Maybe like this

Code:
If Cells(Row, 13).Value > Cells(Row, 12).Value Then
    Cells(Row, 15).Value = Cells(Row, 13) - Cells(Row, 12)
Else
    Cells(Row, 15).Value = -1
End If
 
Upvote 0
Hi Vog,

Sorry I didn't explain this fully.

My dates/times will varied so could be anything between 0 and 12 & the negatives. It's time zone differences.

Jon
 
Upvote 0
You mean retain the value but display as -1? Format the cells beforehand as hh:mm;-1
 
Upvote 0
No minus values can be anything but need to be display as -1 or -2 etc not as ##########.

So for example

L2 = 25/12/2010 12:00:01
M2 = 25/12/2010 13:00:01

The difference between M2 and L2 is -1 hour

L2 = 25/12/2010 09:00:01
M2 = 25/12/2010 13:00:01

The difference -4 hours

but could have
L2 = 25/12/2010 16:00:01
M2 = 25/12/2010 13:00:01

where the difference is +3 hours

Hopefully more clearer.
Jon
 
Upvote 0
this will put the hour difference in column 15
Code:
Cells(Row, 15).Value = 24 * (Cells(Row, 13) - Cells(Row, 12))
With the OP code, its not the value that is showing the #######, it the number format of the cell.
 
Last edited:
Upvote 0
mikerickson,

Thx for response I tried this but got some very strange results. However, found out it was a case of when to multiple by 24 so end result that works is

<code>
Cells(Row, 15).Value = (Cells(Row, 13) - Cells(Row, 12)) * 24
</code>


Thx to all who helped.

Jon
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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