Round up time by quarter of the hour in VBA

Mux99

Board Regular
Joined
Apr 15, 2019
Messages
57
Hello

The macro below works well for converting military time to regular time and adjusts the one difference in timezone. I would like to round up the outputted time by quarter of the hour as I am currently doing it manually after running the macro using the MROUND function. Ex 05:20=05:30 & 20:55=21:00

MATCH62005:20
DATA1310
DATA1540
MATCH215520:55

<tbody>
</tbody>


For Each Cell In Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
If Cell.Value = "MATCH" Then Cell.Offset(, 1).Value = TimeValue(Format(Cell.Offset(, 4).Value, "0\:00")) - TimeValue("01:00")
Next
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about just a formula?

C​
D​
E​
F​
G​
H​
3​
MATCH
620​
5:30​
G3: =IF(C3 <> "MATCH", "", CEILING(TEXT(D3, "0\:00") - "1:00", "0:15"))
4​
DATA
1310​
5​
DATA
1540​
6​
MATCH
2155​
21:00​
 
Upvote 0
How about just a formula?

C​
D​
E​
F​
G​
H​
3​
MATCH
620​
5:30​
G3: =IF(C3 <> "MATCH", "", CEILING(TEXT(D3, "0\:00") - "1:00", "0:15"))
4​
DATA
1310​
5​
DATA
1540​
6​
MATCH
2155​
21:00​

<tbody>
</tbody>

I would like to add it to the macro as there will be multiple conditions. If C equals "MATCH" then round up to the next 15min interval and other values that will need to be rounded up by 10min & 30min intervals depending on the text in column C.
 
Upvote 0
Code:
Sub Mux()
  Dim cell As Range
  Dim r As Range
  
  Set r = FindAll("Match", Columns(3))
  If Not r Is Nothing Then
    For Each cell In r
      cell.Offset(, 4).Value = Ceiling(TimeValue(Format(cell.Offset(, 1).Value, "0\:00")) - TimeValue("1:00"), TimeValue("0:15"))
    Next cell
  End If
End Sub

Public Function FindAll(vWhat As Variant, _
                        rSearch As Range, _
                        Optional LookIn As XlFindLookIn = xlValues, _
                        Optional LookAt As XlLookAt = xlWhole, _
                        Optional MatchCase As Boolean = False) As Range

  Dim cell          As Range
  Dim sAdr          As String

  Set cell = rSearch.Find(What:=vWhat, _
                          LookIn:=LookIn, _
                          LookAt:=LookAt, _
                          MatchCase:=MatchCase, _
                          SearchFormat:=False)
  If Not cell Is Nothing Then
    Set FindAll = cell
    sAdr = cell.Address
    Do
      Set FindAll = Union(FindAll, cell)
      Set cell = rSearch.FindNext(cell)
    Loop While cell.Address <> sAdr
  End If
End Function

Function Ceiling(d As Double, ByVal s As Double) As Double
  Dim q             As Double
  Dim iSgn          As Integer

  If s < 0 Then s = -s
  
  iSgn = Sgn(d)
  If iSgn = -1 Then d = -d

  q = d / s
  If q <> Int(q) Then q = Int(q) + 1#
  Ceiling = iSgn * q * s
End Function
 
Upvote 0
Try this.
Code:
For Each Cell In Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
    If Cell.Value = "MATCH" Then
        Cell.Offset(, 1).Value = Application.Ceiling(TimeValue(Format(Cell.Offset(, 4).Value, "0\:00")) - TimeValue("01:00"), TimeSerial(0, 15, 0))
    End If
Next
 
Upvote 0
Try this.
Code:
For Each Cell In Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
    If Cell.Value = "MATCH" Then
        Cell.Offset(, 1).Value = Application.Ceiling(TimeValue(Format(Cell.Offset(, 4).Value, "0\:00")) - TimeValue("01:00"), TimeSerial(0, 15, 0))
    End If
Next

Thanks for this. The function I ended up using is:
​For Each Cell In Range("C2:C" & Cells(Rows.Count, 3).End(xlUp).Row)
If Cell.Value = "MATCH" Then Cell.Offset(, 4).Value = Application.WorksheetFunction.Ceiling(TimeValue(Format(Cell.Offset(, 1).Value, "0\:00")) - TimeValue("01:00"), TimeValue("00:15"))
Next

Is there any downside to using Application Worksheet Functions as this seems like an easier solution than the answers above?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,972
Members
448,933
Latest member
Bluedbw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top