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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA - Summing Hours and Minutes

Status
Not open for further replies.

ProjectExplorer

Programmer
Mar 22, 2002
95
GB
I am trying to sum a range of cells containing hours and minutes ("hh:mm")in VBA and place the answer in a variable. For example:

20:00
15:00

Result 35:00

I can do this on an excel worksheet using the format "[h]:mm" but it doesn't want to work when I use it in VBA. Any one know how I do it?

Thanks
 
Have you tried to play with the macro recorder ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Summing to a variable will work. Converting it to text is the problem ( well, it is in Excel 97 ), as the Format function doesn't support [hh] hour formats. Use Application.Text to use the worksheet Text function to format the variable to be the string you want.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Many thanks for the suggestion but it doesn't solve the problem - i.e. how to add say 20hrs to 30hrs and display the result (50:00 hrs) on say, a msg box. I cannot figure out what type of variable to use (a variant, long ?) and how to format the answer to appear as 50:00. I'm getting some very strange results at the moment.



 
Say the cumuled time is stored in a Date variable named myDateTime:
MsgBox (24 * Int(myDateTime) + Format(myDateTime, "h")) & Format(myDateTime, ":nn") & " hrs"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Fantastic - I can stop pulling my hair out now.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top