+ Reply to Thread
Results 1 to 7 of 7

SUM range of time

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    SUM range of time

    Hi,

    I have two ranges of time where I need to calculate averages and SUM for both of them, displayed on Userform. One SUM works good, other not, everything else should be OK.

    I guess something is wrong about calculating times over 24 hours period, and I can't fix It. Can somebody tell me what's wrong, and how to fix It ?

    Please check sample and click on Userform, you'll see that SUM for column C is wrong

    Any help appreciated !!
    Attached Files Attached Files
    Last edited by Lukael; 08-25-2015 at 10:21 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: SUM range of time

    Hi there,

    I think you've probably come up against a genuine Excel vs VBA BUG!

    Cell C367 is correctly formatted to display ELAPSED time ("[hh]:mm"), so its value of 2.534... is displayed as 60:50. When formatted as hh:mm the value displayed is 12:50 - i.e. the same as appears on your UserForm.

    BUT - it appears that this NumberFormat is not supported in VBA. I thought it would be simply a matter of changing the NumberFormat in your code to [hh]:mm, but that just doesn't seem to work:

    
    "Total time - C column : " & Format(WorksheetFunction.Sum(Range("C1:C365")), "hh:mm") & " h"

    I'd be quite happy to be proved wrong by any experts out there!

    Regards,

    Greg M
    Last edited by Greg M; 08-25-2015 at 10:41 AM. Reason: Minor addition

  3. #3
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: SUM range of time

    Hi Greg,

    thanks for reply. I Can't believe, but your words are exactly my thoughts. I tried everything with format methods in VBA, but nothing worked, even [h]:mm:ss didn't do nothing.

    I would be happy with any other solution, time format is not necessary, just output on Userform is desirable in "hh:mm". Maybe convert time to numbers and then text strings as a result on Userform ??

    Or just put a cell text value on userfom and hide cell on sheet ?
    Last edited by Lukael; 08-25-2015 at 12:17 PM.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: SUM range of time

    Hi Lukael,
    maybe so
    Private Sub UserForm_Initialize()
    Dim s$, dt#
    s = "Average time - C column : " & Format(WorksheetFunction.Average(Range("C1:C365")), "N:ss") & " min" & vbNewLine & vbNewLine
    dt = WorksheetFunction.Sum(Range("C1:C365"))
    'dt * 24 = 60.8333 hours
    'dtt = Int(dt * 24) & ":" & Round((dt * 24 - Int(dt * 24)) * 60, 0) = "60 h 50 m"
    s = s & "Total time - C column : " & Int(dt * 24) & ":" & Round((dt * 24 - Int(dt * 24)) * 60, 0) & " h" & vbNewLine & vbNewLine
    s = s & " Average time - H column : " & Format(WorksheetFunction.Average(Range("H1:H365")), "N:ss") & " min" & vbNewLine & vbNewLine
    dt = WorksheetFunction.Sum(Range("H1:H365"))
    s = s & " Total time - H column : " & Int(dt * 24) & ":" & Round((dt * 24 - Int(dt * 24)) * 60, 0) & " h"
    TextBox1.Text = s
    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: SUM range of time

    Norie,

    looks like you had an A in mathematics when you were in school, It works

    Can you comment these steps so that I can fully understand what you did ?


    Thanks a lot, that's excellent programming !

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: SUM range of time

    I'm not Norie, don't worry (with all due respect to Norie)
    and there are comments in the post #4
    Last edited by nilem; 08-25-2015 at 01:42 PM.

  7. #7
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: SUM range of time

    Bump, I'm sorry, I was in kind of rush when reading your post. My apologies nilem

    Thanks for help !!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] COUNTIF Time Range - Time Range provided in Cells
    By Eaks77 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-02-2015, 11:12 PM
  2. [SOLVED] Counting values in a time range - when the time range crosses midnight
    By dlocos in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-24-2014, 04:27 AM
  3. Create a Time Range off Time Stamps, and Count Intersecting values
    By athyeh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2014, 05:07 PM
  4. Calculate if Time range appears in an other Time range
    By bajdr47 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-25-2013, 02:50 AM
  5. compare time values - current time with time in cell range?
    By wyattea in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-13-2013, 11:28 PM
  6. send to range, popup box to input what the range should be each time/select range
    By lax2734 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2012, 01:37 AM
  7. Display the date and time each time the data in a cell range is changed
    By BVZM in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-24-2012, 01:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1