Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I simulate Excel's Ceiling/Floor Functions in VBA?

Functions

How do I simulate Excel's Ceiling/Floor Functions in VBA?

by  ByteMyzer  Posted    (Edited  )
To simulate these two Excel functions in Access Visual Basic, paste the following code into a new module and save it:

Code:
Public Function Ceiling(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' is the multiple to which you want to round
    Ceiling = (Int(X / Factor) - (X / Factor - Int(X / Factor) > 0)) * Factor
End Function

Public Function Floor(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' is the multiple to which you want to round
    Floor = Int(X / Factor) * Factor
End Function


Examples

Ceiling(2.5, 1) equals 3
Ceiling(2.5) equals 3
Ceiling(-2.5, -2) equals -4
Ceiling(-2.5, 2) equals -2
Ceiling(1.5, 0.1) equals 1.5
Ceiling(0.234, 0.01) equals 0.24

Floor(2.5, 1) equals 2
Floor(2.5) equals 2
Floor(-2.5, -2) equals -2
Floor(-2.5, 2) equals -4
Floor(1.5, 0.1) equals 1.5
Floor(0.234, 0.01) equals 0.23
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top