INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA and Custom Functions

Rounding functions by Craig0201
Posted: 4 Dec 01

A pair of rounding functions.  Work identically to those in Excel.

Function Round(Number As Double, Num_Digits As Integer) As Double

'Set up variables
Dim dblNumNum_Digits As Double
Dim intNumNum_Digits As Integer

'Move decimal point such that int will perform at correct level
dblNumNum_Digits = Number * (10 ^ Num_Digits)

'Integerise
intNumNum_Digits = Int(dblNumNum_Digits)

'Compare integer to see if needs rounding up
If dblNumNum_Digits - intNumNum_Digits >= 0.5 Then
    'round up
    intNumNum_Digits = intNumNum_Digits + 1
Else
    'round down
    intNumNum_Digits = intNumNum_Digits
End If

'Return result, moving decimal point back
Round = intNumNum_Digits / (10 ^ Num_Digits)

End Function

Function MRound(Number As Double, Multiple As Double) As Double

'Set up variables
Dim dblDivided As Double
Dim dblIntDivided As Double

'Divide
dblDivided = Number / Multiple
'Integerise
dblIntDivided = Int(dblDivided)

'Round dblDivided to nearest whole number in intDivided
dblIntDivided = dblIntDivided + Round(dblDivided - dblIntDivided, 0)

'Return result, returning to nearest multiple
MRound = dblIntDivided * Multiple

End Function

Hope they help!

Craig

Back to Microsoft: Access Other topics FAQ Index
Back to Microsoft: Access Other topics Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close