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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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
[table="width:, class:grid"][tr][td="bgcolor:#c0c0c0"][/td][td="bgcolor:#c0c0c0"]
a​
[/td][td="bgcolor:#c0c0c0"]
b​
[/td][td="bgcolor:#c0c0c0"]
c​
[/td][/tr][tr][td="bgcolor:#c0c0c0"]
1​
[/td][td]
250.00​
[/td][td="bgcolor:#ccffcc"]
249.0​
[/td][td="bgcolor:#ccffcc"]b1: =floor(a1, 10) + lookup(mod(a1, 10), {0,1,3.5,7}, {-1,2,5,9})[/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
2​
[/td][td]
250.99​
[/td][td="bgcolor:#ccffcc"]
249.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
3​
[/td][td]
251.00​
[/td][td="bgcolor:#ccffcc"]
252.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
4​
[/td][td]
253.49​
[/td][td="bgcolor:#ccffcc"]
252.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
5​
[/td][td]
253.50​
[/td][td="bgcolor:#ccffcc"]
255.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
6​
[/td][td]
256.99​
[/td][td="bgcolor:#ccffcc"]
255.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
7​
[/td][td]
257.00​
[/td][td="bgcolor:#ccffcc"]
259.0​
[/td][td][/td][/tr]
[tr][td="bgcolor:#c0c0c0"]
8​
[/td][td]
260.00​
[/td][td="bgcolor:#ccffcc"]
259.0​
[/td][td][/td][/tr]
[/table]
 
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,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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