Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i need a formula please? 1

Status
Not open for further replies.

ridge

Technical User
Jan 31, 2000
91
US
I need a formula that will find the differance between hours.
In cell A1, I will have a date & time. 1/17/00 1300
In cell A2, I will have another............1/18/00 1500
The answer here is obviously 26 hours, but i need the formula.

Thanks in Advance
 
Format cells A1 and B1 for date and military time.
Format the result cell as General.

Go to the VB editor, add a module and insert this function:
Code:
Public Function FindDelta(dStart As Date, dEnd As Date) As String
    Dim lVal As Long
    lVal = DateDiff("n", dStart, dEnd)
    FindDelta = Format(lVal / 60, "##.##")
End Function
In the results cell, insert this formula:
Code:
=finddelta(A1,B1)
Hope this helps!
 
Thank you very much DSI, but it seems as though I don't know as much about
this as i should. I am totally lost when you got me to the VB Editor. and the Public Function. I thank you again if you can be a little more specific.
 
The Public Function is a customized function that you can use, similar to those included with Excel. I just wrote it myself. If you insert it into the workbook (explained below) it will be available in all of the sheets in that book. One thing to note, you may be prompted to Disable/Enable Macros when you first open the workbook. You will need to Enable the macros for the function to work.

From the worksheet:
1. Tools > Macro > Visual Basic Editor
2. Insert > Module
3. Copy and Paste the code above (Public Function ... End Function)
4. File > Close and Return to Microsoft Excel
5. Insert the function call in the desired cell

About the function:
Code:
Public Function FindDelta(dStart As Date, dEnd As Date) As String
    Dim lVal As Long
    lVal = DateDiff("n", dStart, dEnd)
    FindDelta = Format(lVal / 60, "##.##")
End Function
FindDelta needs to be passed 2 dates and returns the calculated difference. The DateDiff function (VBA) returns the difference between the two values in minutes ("n"). I just convert that to decimal hours (Format() function) so that it will return a value in hours, to 2 decimal places.

Now, when you call this function from a cell, you just have to specify the cells that contain the start and end dates.

Example:
cell F23 is the start date/time
cell M192 is the end date/time
If you want the difference displayed in cell B8, then insert the following formula into cell B8 (General format):
=finddelta(F23,M192)

Let me know if you have any more problems. I can e-mail you a workbook with this function so you can review it for yourself.


 
DSI: You'll have to excuse me if I'm just ignorant, but why are you even using VBA?

The problem, as I see it, is that Row 1 and 2 should be formatted as time, while row 3 should be formatted as follows:

[h]:mm
techsupportgirl@home.com
Brainbench MVP for Microsoft Word
 
Dreamboat:

Wouldn't that be a problem since the cells span over different days? If the cells were formatted as time, the date would not show.

Please, correct me if I am wrong!

Thanks.
 
Test it out, DSI. The problem most people have is either:

a) they don't enter the dates so Excel cannot resolve the 24-hour issue.

b) they format all the cells as regular old time cells and expect it to be able to revolve around the clock.

Look at the begin and end time as being a number that is more like a zip code. It's used for reference but it's not quantitative by itself. Does that make sense? I wanted it to sound technical, but it sounds like BS!!
techsupportgirl@home.com
Brainbench MVP for Microsoft Word
 
Good answer! I have never seen, and obviously never used, the elapsed time formatting. I was trying h:mm and was getting #VALUE as a result. They say you learn something new every day, so I guess it's time for me to go home!

Thanks!
 
Thanks to both of you for your help. However I guess I am the one here who is ignorant. I can't get nothing to work.
To be very honest, the visual editor is wayyyy over my head.
I have never had any formal classes on excel. Everything I know is self taught, and I thought I was doing pretty good untl now.(laff)...I consider myself well to do as far as basic formulas, "IF" formulas, pivot tables, etc.
I'l certainly willing to keep trying any ting either of you can say to help.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top