Rounding up to nearest higher integer in VBA

If using a WorksheetFunction object to access a ROUNDUP or CEILING function is off the table then the same can be accomplished with some maths.

Number of layers = Int(iQty / iLayerQty) - CBool(Int(iQty / iLayerQty) <> Round(iQty / iLayerQty, 14))

A VBA True is the equivalent of (-1) when used mathematically. The VBA Round is there to avoid 15 digit floating point errors.


I don't see any reason to avoid WorksheetFunction; I don't see any confusion here.

Number_of_layers = WorksheetFunction.RoundUp(iQty / iLayerQty, 0)

You could also roll your own function:

Function RoundUp(ByVal Value As Double)
    If Int(Value) = Value Then
        RoundUp = Value
    Else
        RoundUp = Int(Value) + 1
    End If
End Function

Call it like this:

Number_of_layers = RoundUp(iQty / iLayerQty)

I use -int(-x) to get the ceiling.

?-int(-1.1)  ' get ceil(1.1)
2

?-int(1.1)   ' get ceil(-1.1)
-1

?-int(-5)    ' get ceil(5)
5

Tags:

Excel

Vba