Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding Date and Time in VBA

Tags:

excel

vba

How can I round off date and time in excel using VBA?

For example, the user selects the value from the calendar which is copied in Cell A6 = "08/25/2016 09:02:00"

I am pulling the data in 15 minutes interval so I want it to be A6 = "08/25/2016 09:00:00"

So if the user selects any date and time that is not in multiple of 15 minutes, it should go back to the previous 15 minute interval value and pull the data.

like image 629
Ahsan Khan Avatar asked Aug 25 '16 21:08

Ahsan Khan


People also ask

How do you round in VBA?

Example #1 – VBA Round Function to Round a NumberStep 1: Insert a new module under Visual Basic Editor (VBE). Step 2: Define a new sub-procedure to store a macro in VBE. Step 4: Now, add “& Round (10.9834, 2)” in front of MsgBox command, so that the rounded value will be shown up in the message box along.

How do I fix the date format in Excel VBA?

Click on Insert tab > select Module. Step 2: Write the subprocedure for VBA Format Date or choose anything to define the module. Step 3: Choose the range cell first as A1. Step 4: Then use the Number Format function as shown below.

Does VBA round up or down?

The Round function in VBA limited in that it will always either round up or round down – depending on the number – so 19.58786 will round UP to 19.59 whereas 19.58246 will round down to 19.58 – the trick is to have a look at the number in 3rd place after the decimal – if that number is 5 or greater, it will always ...

What is CDEC in VBA?

CDEC is a built-in data type conversion function available as a VBA function. VBA CDEC function converts a data type from any other data type to a decimal data type. It stands for “Convert to Decimal.”


2 Answers

Pull out the minutes, floor the date portion to get rid of the time, then add it back by building it with TimeSerial:

Private Sub Example()
    Dim foo As Date
    foo = CDate("08/25/2016 09:02:00")

    Dim minutes As Long
    minutes = Minute(foo)
    minutes = 15 * (minutes \ 15)  'Round down by 15 minute increments

    foo = Int(foo) + TimeSerial(Hour(foo), minutes, 0)
    Debug.Print foo
End Sub

Edit: Like @Pekka mentions, this can be done with a worksheet formula too - this is the equivalent to the code VBA above:

=INT(A6)+TIME(HOUR(A6),INT(MINUTE(A6) / 15) * 15, 0)
like image 177
Comintern Avatar answered Sep 18 '22 22:09

Comintern


VBA is not necessary. This can be done directly in Excel. =FLOOR(A6,TIME(0,15,0)) will truncate a date time value to the previous 15 minute value.

Excel represents date values as a floating point value since an initial date (around 1900, depending on version) with the time as the fractional portion of the value.

You could, of course, use the same expression in VBA code in the same way.

As Jeeped comments, this is a more self-documenting alternative to the more direct expression =int(A6*24*4)/4/24 initially suggested.

like image 25
Pekka Avatar answered Sep 20 '22 22:09

Pekka