# Excel macro time comparison



## Mzun (Jun 29, 2011)

I'm trying to write a macro that calculates the difference between two times. If the difference is greater that 8 hours then the diffence is set equal to 8 hours. My code currently looks like this but it doesn't work (cell A1 stores the difference between the two times).

If Range("A1").Value > 8 Then
Range("A1").Value = 8
End If

Any Suggestions?


----------



## andythepandy (Jul 2, 2006)

Take a look at the datediff function - http://office.microsoft.com/en-us/excel-help/datediff-function-HA001228811.aspx

Andy


----------



## meynardo (Sep 20, 2011)

Did you try:

=IF(A1>8,8,A1)

Or give us more details on how your spreadsheet is organized.


----------



## henry48 (Sep 27, 2011)

A macro is a series of commands that perform a specific task in Excel. To save time when performing common operations you can create a macro with the macro recorder that can be run whenever you need to perform the task. This tutorial describes step by step how to create a simple macro in Excel using the macro recorder.


----------

