[Solved] Time Formulas

ben.cote

New member
Hi,

I need a formula that will look at the data range and return the time when the first data was entered and the time when the last data was entered. It must also calculate the total number of hours. The start and end times need to be rounded to the nearest 15 minutes and the total time needs to be rounded up to the nearest hour.

CIAO
 

Attachments

Hi,

I need a formula that will look at the data range and return the time when the first data was entered and the time when the last data was entered. It must also calculate the total number of hours. The start and end times need to be rounded to the nearest 15 minutes and the total time needs to be rounded up to the nearest hour.

CIAO
Dear Ben.cote,
Thanks for posting on our Exceldemy Forum. According to your requirements, I have developed an event-driven VBA code that will record the data entry time when any cell value is changed on a specified range [inputRange = Range("C9:N16")]. The VBA code is given below:
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim inputRange As Range
    Dim Start_Time As Range
    Dim End_Time As Range
    Dim Total_Time As Range
    Dim time_difference As Double

    ' Set the ranges
    Set inputRange = Range("C9:N16")
    Set Start_Time = Range("B2")
    Set End_Time = Range("B3")
    Set Total_Time = Range("B4")
    ' Check for intersection and non-blank value
    If Not Application.Intersect(Target, inputRange) Is Nothing And Target.Cells.Count = 1 And Target.Cells(1, 1).Value <> "" Then

        If IsEmpty(Start_Time.Value) Then
            Start_Time.Value = RoundTo15Minutes(Now())
            End_Time.ClearContents
            Total_Time.ClearContents
        Else
            End_Time.Value = RoundTo15Minutes(Now())
        End If

        If Not IsEmpty(End_Time.Value) And Not IsEmpty(Start_Time.Value) Then
            time_difference = End_Time.Value - Start_Time.Value
            Total_Time.Value = ConvertToHours(time_difference)
        End If
    End If
End Sub

Sub Clear_Time()
Range("B2:B4").ClearContents
End Sub

Function RoundTo15Minutes(ByVal inputDateTime As Date) As Date
    Dim roundedDateTime As Date
    Dim hours As Integer
    Dim minutes As Integer
    
    ' Extract the time components
    hours = Hour(inputDateTime)
    minutes = Minute(inputDateTime)
    
    ' Round to the nearest 15 minutes
    minutes = Round(minutes / 15) * 15
    
    ' Handle the case where rounding goes to the next hour or day
    If minutes = 60 Then
        hours = hours + 1
        minutes = 0
    End If
    
    ' Handle the case where rounding goes to the next day
    If hours = 24 Then
        hours = 0
        ' Increment the date by 1 day
        inputDateTime = inputDateTime + 1
    End If
    
    ' Construct the rounded date and time using TimeSerial and DateSerial
    roundedDateTime = TimeSerial(hours, minutes, 0) + DateSerial(Year(inputDateTime), Month(inputDateTime), Day(inputDateTime))
    
    ' Return the rounded date and time
    RoundTo15Minutes = roundedDateTime
End Function

Function ConvertToHours(ByVal inputTime As Date) As Double
    Dim hours As Double
    
    ' Extract the hour and minute components
    Dim hourComponent As Integer
    Dim minuteComponent As Integer
    hourComponent = Hour(inputTime)
    minuteComponent = Minute(inputTime)
    
    ' Convert to hours and round off to the nearest hour
    hours = hourComponent + minuteComponent / 60
    hours = Round(hours)
    
    ' Return the result
    ConvertToHours = hours
End Function
Here is an illustration of how the code works:

Entry Time-output-min.gif

It will only record the entry time of the cells that belongs to the inputRange.
Kindly change the inputRange according to your data. Feel free to download the updated file and review the code and contact us for further assistance.
Regards
Aniruddah
Team Exceldemy
 

Attachments

Thank you Aniruddah for your VBA solution. I cannot use a VBA solution in this workbook. Any other solutions outside of VBA?
 
Thank you Aniruddah for your VBA solution. I cannot use a VBA solution in this workbook. Any other solutions outside of VBA?
I'm sorry, but the requirements you have stated in your post cannot be achieved using built-in Excel formulas or functions. Your requirement is dynamic, and therefore, using VBA is the only solution that comes to my mind. If you are not familiar with VBA, you can read this introductory article on our blog.

Regards
Aniruddah
 
Hi Aniruddah,

Thank you again for your solution. I did get a formula to work. I used one helper cell.
 

Attachments

  • Screenshot 2023-12-06 162354.png
    Screenshot 2023-12-06 162354.png
    5.5 KB · Views: 19

Online statistics

Members online
0
Guests online
3
Total visitors
3

Forum statistics

Threads
355
Messages
1,556
Members
662
Latest member
Pendyala Vignesh
Back
Top