Rounding to a multiple of 2, 5 or 9

KellyBailey

New Member
Joined
Jul 17, 2018
Messages
5
Hello,

Is there any way that I can use a formula to convert a number to a multiple of 2, 5 or 9?
I need it to round to the closest ideally.

For example:

252.9 = 252
255.8 = 255
260 = 259

Many thanks in advance
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If i understand correctly, you don't actually want the number to be converted to a multiple of 2, 5, or 9 because in your example (260 = 259), that is incorrect, the number would stay at 260 since it's a multiple of 2. So I'm assuming you want the last digit to change to either a 2, 5, or 9 depending on which direction is closer to the current number. Below is a formula I developed that will handle that. In the developer tab open up Visual Basic, click Insert->Module, then paste in the below code. Go back into the Excel and type in a cell "=RoundTo_2_5_9('Select Cell'). Let me know if this works for you.

Code:
Public Function RoundTo_2_5_9(ByVal dValue As Double) As Long
    
    Dim dRounded As Double
    Dim dTempValue As Double
    Dim iMinValue As Integer
    
    iMinValue = 10
    
    dRounded = Application.WorksheetFunction.RoundDown(dValue, -1)
    dTempValue = dValue - dRounded
    
    If dTempValue < 0.5 Then
        RoundTo_2_5_9 = dRounded - 1
    ElseIf dTempValue >= 7 Then
        RoundTo_2_5_9 = dRounded + 9
    ElseIf dTempValue < 7 And dTempValue >= 3.5 Then
        RoundTo_2_5_9 = dRounded + 5
    ElseIf dTempValue < 3.5 And dTempValue >= 0.5 Then
        RoundTo_2_5_9 = dRounded + 2
    End If
    
End Function
 
Upvote 0
Sorry the above code works but I forgot to delelte the variable I no longer needed. Below is a shorter version of the code.

Code:
Public Function RoundTo_2_5_9(ByVal dValue As Double) As Long
    
    Dim dRounded As Double
    Dim dTempValue As Double
    
    dRounded = Application.WorksheetFunction.RoundDown(dValue, -1)
    dTempValue = dValue - dRounded
    
    If dTempValue < 0.5 Then
        RoundTo_2_5_9 = dRounded - 1
    ElseIf dTempValue >= 7 Then
        RoundTo_2_5_9 = dRounded + 9
    ElseIf dTempValue < 7 And dTempValue >= 3.5 Then
        RoundTo_2_5_9 = dRounded + 5
    ElseIf dTempValue < 3.5 And dTempValue >= 0.5 Then
        RoundTo_2_5_9 = dRounded + 2
    End If
    
End Function
 
Upvote 0
a​
b​
c​
1​
250.00​
249.0​
b1: =floor(a1, 10) + lookup(mod(a1, 10), {0,1,3.5,7}, {-1,2,5,9})
2​
250.99​
249.0​
3​
251.00​
252.0​
4​
253.49​
252.0​
5​
253.50​
255.0​
6​
256.99​
255.0​
7​
257.00​
259.0​
8​
260.00​
259.0​
 
Upvote 0
Hello,

Thanks both. Unfortunately I couldnt get the VBA one to work, although my experience of this is very low, so I am sure it was probably that I was doing something wrong.

The formula works a treat - thank you.

Best wishes

Kelly
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top