Introduction | Example | Tutorial | Applications
Introduction - VBA RoundUp
The VBA RoundUp function is actually not a VBA function at all. It’s an Excel Worksheet Function that you call by using the WorksheetFunction object. By calling the RoundUp worksheet function from VBA, you’ll be able to round up your numbers to however many digits you like, just like in Excel.
Earlier this month, I taught you everything you need to know about the VBA Round function. Today I’m going to show you how to use the RoundUp function in your VBA macros.
Example - VBA RoundUp
VBA RoundUp to Round Numbers Up
Sub VBA_RoundUp() Dim d1 As Double, d2 As Double d1 = 1.3 d2 = Application.WorksheetFunction.RoundUp(d1, 0) 'equals 2 End Sub
Make powerful macros with our free VBA Developer's Kit
There's a lot to unpack here. To save time and become really good at VBA, make sure you get our free VBA Developer's Kit below. It's full of shortcuts, tips and pre-built macros to make writing VBA easier.
This is a basic macro. It sets the variable
Application.WorksheetFunction object. The final result is that 1.3 is rounded up to 2.
If you’re ready to learn more, the following section shows you how to use RoundUp, including an interesting discussion on the second argument.
Tutorial - VBA RoundUp
Just like the VBA Round function, the RoundUp function accepts two arguments.
- The first argument is the number you want to round.
- The second argument is the number of places you want to round to.
Both arguments are required.
Notice I didn’t say the “number of decimal places” you want to round to when describing the second argument. That’s because the second argument has some interesting behavior.
RoundUp Second Argument
If the second argument equals 0, the number is rounded up to the nearest integer. This what I demonstrated in the example macro.
If the second argument is greater than 0, the number is rounded up to the specified number of decimal places AFTER the decimal.
If the second argument is less than 0, the number is rounded up to the specified digit BEFORE the decimal point.
That sounds unusual, right? Let’s take a look at some examples to clear things up.
More RoundUp Examples
RoundUp After the Decimal Point
d2 = Application.WorksheetFunction.RoundUp(2121.3242, 1)
d2 = Application.WorksheetFunction.RoundUp(2121.3242, 2)
d2 = Application.WorksheetFunction.RoundUp(2121.3242, 3)
In each instance, the variable was rounded up to the specified decimal point.
RoundUp Before the Decimal Point
This is where things get interesting.
d2 = Application.WorksheetFunction.RoundUp(2121.3242, -1)
d2 = Application.WorksheetFunction.RoundUp(2121.3242, -2)
d2 = Application.WorksheetFunction.RoundUp(2121.3242, -3)
That’s pretty cool, isn’t it? Since the VBA RoundUp function is really an Excel worksheet function, the same rules apply when you call the RoundUp function in your spreadsheet. Perhaps it’s common knowledge, but I’m here to admit that until recently, I had no idea you could round numbers to the LEFT of the decimal point in Excel.
I rarely find a need to always round a number up, but I want to know what applications you all are doing where you need to round up. Let me know in the comments section!
That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.