Keven Denen recently posted a couple of useful functions for rounding up or
down to a multiple of a factor. His function for rounding up was as follows
(slightly amended to suit this scenario)
Public Function Ceiling(ByVal dNumber As Double, _
Optional ByVal dFactor As Double = 1) As Double
Ceiling = (Int(dNumber / dFactor) - _
(dNumber / dFactor - Int(dNumber / dFactor) > 0)) * dFactor
End Function
By calling this in another function you can cater for rounding to the nearest
10 or 50 by getting the integer of the base 10 logarithm of the number to
determine its size and therefore the factor to which it should be rounded:
Public Function GetRoundedVal(n As Double)
Dim intLog As Integer
Dim intFactor As Integer
intLog = Int(Log(n) / Log(10))
Select Case intLog
Case Is < 2
intFactor = 10
Case 2
intFactor = 50
Case 3
intFactor = 500
Case 4
intFactor = 5000
End Select
GetRoundedVal = Ceiling(n, intFactor)
End Function
In the above I've extended your pattern so that 1001 – 1500 would be rounded
to 1500, 1501-2000 to 2000, 10001 – 15000 to 15000, 15001-20000 to 20000 and
so on up to a maximum of 100000, but you can easily amend it if necessary.
You'd then simply call the second function to get the rounded value:
GetRoundedVal([The Number])
Ken Sheridan
Stafford, England
I have a range of values that I'd like to round. For example for 1- 10, I
want to return 10, 11-20 = 20 then at 101-150=150, 151-200=200. Each record
is a single number. What would be the best expression to use?