I'm an Accounting Manager at a large steel company. I need the most versatile rounding function I can get.
I found this function and added the "nearest" function so it can now round any way you could possibly want...UP..DOWN..NEAREST. It mimicks MS Excel rounding so you'll never have to explain any differences there.
Example of "rounding up" to the closest nickel or dime in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],.05,"UP")
Example of rounding to the nearest 100 in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],100,"NEAREST")
ENJOY!!!!!!!
Public Function RND_TO_NEAREST(Amt As Variant, Divisor As Variant, DIR_UP_DN_NEAREST As String) As Variant
On Error Resume Next
Dim Temp As Variant
Temp = (Amt / Divisor)
If Int(Temp) = Temp Then
RND_TO_NEAREST = Amt
Exit Function
Else
Select Case UCase(DIR_UP_DN_NEAREST)
Case "UP"
Temp = Int(Temp) + 1
RND_TO_NEAREST = Temp * Divisor
Exit Function
Case "DN"
Temp = Int(Temp)
RND_TO_NEAREST = Temp * Divisor
Exit Function
Case "NEAREST"
Temp = Round(Amt / Divisor) * Divisor
RND_TO_NEAREST = Temp
Exit Function
Case Else
Exit Function
End Select
End If
End Function
Thanks,
Doug
I found this function and added the "nearest" function so it can now round any way you could possibly want...UP..DOWN..NEAREST. It mimicks MS Excel rounding so you'll never have to explain any differences there.
Example of "rounding up" to the closest nickel or dime in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],.05,"UP")
Example of rounding to the nearest 100 in a query: FIELD_NAME: RND_TO_NEAREST([PRICE],100,"NEAREST")
ENJOY!!!!!!!
Public Function RND_TO_NEAREST(Amt As Variant, Divisor As Variant, DIR_UP_DN_NEAREST As String) As Variant
On Error Resume Next
Dim Temp As Variant
Temp = (Amt / Divisor)
If Int(Temp) = Temp Then
RND_TO_NEAREST = Amt
Exit Function
Else
Select Case UCase(DIR_UP_DN_NEAREST)
Case "UP"
Temp = Int(Temp) + 1
RND_TO_NEAREST = Temp * Divisor
Exit Function
Case "DN"
Temp = Int(Temp)
RND_TO_NEAREST = Temp * Divisor
Exit Function
Case "NEAREST"
Temp = Round(Amt / Divisor) * Divisor
RND_TO_NEAREST = Temp
Exit Function
Case Else
Exit Function
End Select
End If
End Function
Thanks,
Doug