Formula or VBA to "round" numbers for sales prices + issues with number/text/...

_SGX_

New Member
Joined
Feb 3, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Dear Forum,

It's been a while since I had to use the Excel wizards of this forum, but I can't wrap my head around an easy to use formula or VBA to "round" sales prices of a starting company that is obligated by (belgian) law to change the prices to specific decimals/roundings, because 1ct, 2ct,... isn't allowed to have people pay like that anymore, minimum is a 5ct.

Here's the list:
,00 ,01 ,02 --> ,00
,03 ,04 --> increase to ,05
,06 ,07 --> decrease to ,05
,08 ,09 --> increase to ,10

Examples:
3,99 = 4,00
3,32 = 3,30
3 = 3,00 = OK
3,2 = 3,20 = OK

My solution (not yet finished even) has multiple steps, multiple formulas, multiple IFs, trying to extract the last digit to see what needs to be done but I cannot get the decimals to always stay right, I had trouble getting my lookups to use digits instead of numbers as text,... Hopefully I'm missing some awesome formula to get me going in the right direction.

Some of the steps:
1. Make prices TEXT, so other manipulations can be done more easily
2. Editing the numbers that are already without 1 or 2 decimals (pretty much manually or with "if(len(A1)=3;A1&"0"): 3 --> 3,00 3,2 --> 3,20
--> But I think it would work better to try and find the comma, and then work from the number to the right of it (decimal digits)
3. Extracting last "number" using Value() of Right(A1;1)
4. IF formula to check if it's ending in 0,1,2,3 --> Becomes 0
5. IF Formula to check if it's ending in 4,5,6,7--> Becomes 5
6. IF Formula to check if it's ending in 8,9 --> Becomes 10
7. Adding the new ending digits to the starting numbers, seems to function except for no decimal numbers in start... somehow my formula makes 30 out of starting 3.
--> Using multiple IF statements to calculate what needs to be done. Example (but it's in Dutch Excel- =ALS(G3=9;D3+0,01;ALS(G3=8;D3+0,02;ALS(G3=0;D3;LINKS(D3;LENGTE(D3)-1))&G3))

Example Mini-sheet (this is new to me, seems awesome for assistance!)
Prijswijziging_Berdy_01.01.22.xlsx
ABCDEFGH
1Purchase PriceExtra column for calculating pricesSales PriceExtract last number,00-,02 --> ,00,03-,07 --> ,05,08-,09 --> ,10 + Adding up the numbers to original
2
37,1011,4511,9555511,95
46,3510,2410,7444510,75
519,5231,5032000320
68,4613,6514,1555514,15
730,0748,5149,0110049,010
810,7717,3817,8888817,90
96,6410,7111,2110011,210
100,000,000,50000,50
118,7814,1714,6777514,65
1224,4139,3739,8777539,85
1396,98156,45156,95555156,95
146,5110,5011000110
1522,1335,7036,2000036,200
16107,06172,73173,23335173,25
177,3211,8112,3110012,310
1827,8944,9945,4999945,50
195,398,709,200009,200
2012,3319,8920,3999920,40
217,2911,7612,2666512,25
2226,0041,9542,4555542,45
237,1611,5412,0444512,05
2424,4739,4839,9888840,00
257,1611,5412,0444512,05
2624,4739,4839,9888840,00
278,2413,2913,7999913,80
2831,8651,4051,9000051,900
297,1611,5612,0666512,05
3028,3145,6846,1888846,20
317,6112,2812,7888812,80
329,9216,0116,5110016,510
3331,7351,2051,7000051,700
347,9112,7513,2555513,25
3526,0041,9542,4555542,45
367,9712,8613,3666513,35
3725,0640,4340,9333540,95
383,946,356,855556,85
394,907,908,400008,400
408,0713,0213,5220013,520
413,876,246,744456,75
427,1311,5012000120
434,857,828,322008,320
4421,8035,1735,6777535,65
454,437,147,644457,65
466,8110,9911,4999911,50
473,916,306,800006,800
485,969,6110,1110010,110
4917,9028,8729,3777529,35
504,407,107,600007,600
514,497,257,755557,75
5212,3719,9520,4555520,45
539,7315,6916,1999916,20
5411,6918,8519,3555519,35
5529,9448,3048,8000048,800
56209,57338,10338,60000338,600
5715,2024,0024,5000024,500
Blad1
Cell Formulas
RangeFormula
D3:D57D3=IF(LEN(C3)<=3,VALUE("0"),VALUE(RIGHT(C3,1)))
E3:E57E3=IF(OR(D3=0,D3=1,D3=2),VALUE("0"),D3)
F3:F57F3=IF(OR(E3=3,E3=4,E3=5,E3=6,E3=7),VALUE("5"),E3)
G3:G57G3=IF(F3=9,C3+0.01,IF(F3=8,C3+0.02,IF(F3=0,C3,LEFT(C3,LEN(C3)-1))&F3))
A3:A56A3=I3*0.75
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I think you have a bracket in the wrong place n your "G" column formula.
If you remove a bracket after the "-1" and place it at the end of the formula ( so it looks like below) I think it should do what you are after

=IF(F3=9,C3+0.01,IF(F3=8,C3+0.02,IF(F3=0,C3,LEFT(C3,LEN(C3)-1)&F3)))
 
Upvote 0
Hey Gordsky, thank you for replying! It certainly helped! The only issue seems to remain with the ,01!

Prijswijziging_Berdy_01.01.22.xlsx
ABCDEFGH
1Purchase PriceExtra column for calculating pricesSales PriceExtract last number,00-,02 --> ,00,03-,07 --> ,05,08-,09 --> ,10 + Adding up the numbers to original
2
37,1011,4511,9555511,95
46,3510,2410,7444510,75
519,5231,503200032,00
68,4613,6514,1555514,15
730,0748,5149,0110049,01
810,7717,3817,8888817,90
96,6410,7111,2110011,21
100,000,000,50000,50
118,7814,1714,6777514,65
1224,4139,3739,8777539,85
1396,98156,45156,95555156,95
146,5110,501100011,00
1522,1335,7036,2000036,20
16107,06172,73173,23335173,25
177,3211,8112,3110012,31
1827,8944,9945,4999945,50
195,398,709,200009,20
2012,3319,8920,3999920,40
217,2911,7612,2666512,25
2226,0041,9542,4555542,45
237,1611,5412,0444512,05
2424,4739,4839,9888840,00
257,1611,5412,0444512,05
2624,4739,4839,9888840,00
278,2413,2913,7999913,80
2831,8651,4051,9000051,90
297,1611,5612,0666512,05
3028,3145,6846,1888846,20
317,6112,2812,7888812,80
329,9216,0116,5110016,51
3331,7351,2051,7000051,70
347,9112,7513,2555513,25
3526,0041,9542,4555542,45
367,9712,8613,3666513,35
3725,0640,4340,9333540,95
383,946,356,855556,85
394,907,908,400008,40
408,0713,0213,5220013,52
413,876,246,744456,75
427,1311,501200012,00
434,857,828,322008,32
4421,8035,1735,6777535,65
454,437,147,644457,65
466,8110,9911,4999911,50
473,916,306,800006,80
485,969,6110,1110010,11
4917,9028,8729,3777529,35
504,407,107,600007,60
514,497,257,755557,75
5212,3719,9520,4555520,45
539,7315,6916,1999916,20
5411,6918,8519,3555519,35
5529,9448,3048,8000048,80
56209,57338,10338,60000338,60
5715,2024,0024,5000024,50
Blad1
Cell Formulas
RangeFormula
D3:D57D3=IF(LEN(C3)<=3,VALUE("0"),VALUE(RIGHT(C3,1)))
E3:E57E3=IF(OR(D3=0,D3=1,D3=2),VALUE("0"),D3)
F3:F57F3=IF(OR(E3=3,E3=4,E3=5,E3=6,E3=7),VALUE("5"),E3)
G3:G57G3=IF(F3=9,C3+0.01,IF(F3=8,C3+0.02,IF(F3=0,C3,LEFT(C3,LEN(C3)-1)&F3)))
A3:A56A3=I3*0.75
 
Upvote 0
can you point me to a row or cell where the error is occuring and state what it should be
 
Upvote 0
Having studied your code it is going to fail in a number of places.
Col D formula is counting the number of char in the cell (you have said LEN) so is calculating on that. Using the right function will ultimately fail because it wont recognise 32.00 as 32.0 but more as 32.

secondly your formula in D says <=3 but your initial text say is should be for <3 and 3 & 4 do something else

lastly when i copied your book to an excel file it would seem that column c is a mix of text and numbers..

Do you actually need all those helper columns also?? If column c was purely numerical you could do it in a single formula
 
Upvote 0
the below single code works fine on a test sheet. If you do want the "helper" columns then simply break the formula down at each "if" statement and paste it into the relevant column.

Excel Formula:
=IF(OR(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=5),C3,IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<3),C3--TRUNC(C3,1),IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>=3,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<=7),C3+(0.05-(C3-TRUNC(C3,1))),ROUND(C3,1))))

In summary the above does the following.
If 2nd dp is 0 or 5 then it doesn't change
if 2nd dp is 1 to 3 then it becomes 0
if 2nd dp is 4 to 7 it becomes 5
if 2nd dp is 8 or 9 it will round up to 1st dp
 
Upvote 0
the below single code works fine on a test sheet. If you do want the "helper" columns then simply break the formula down at each "if" statement and paste it into the relevant column.

Excel Formula:
=IF(OR(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=5),C3,IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<3),C3--TRUNC(C3,1),IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>=3,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<=7),C3+(0.05-(C3-TRUNC(C3,1))),ROUND(C3,1))))

In summary the above does the following.
If 2nd dp is 0 or 5 then it doesn't change
if 2nd dp is 1 to 3 then it becomes 0
if 2nd dp is 4 to 7 it becomes 5
if 2nd dp is 8 or 9 it will round up to 1st dp
Looks awesome. Need to try and use it in the file... Because it's english formulas :/ Could I use the XL2BB addon for that maybe?

Yes of course I don't need the helper columns, they were for me to figure out how to get it to work hehe. I was assuming that in a final formula it would need to check if there is a comma and then work from there (just thinking out loud)
 
Upvote 0
the below single code works fine on a test sheet. If you do want the "helper" columns then simply break the formula down at each "if" statement and paste it into the relevant column.

Excel Formula:
=IF(OR(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=5),C3,IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<3),C3--TRUNC(C3,1),IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>=3,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<=7),C3+(0.05-(C3-TRUNC(C3,1))),ROUND(C3,1))))

In summary the above does the following.
If 2nd dp is 0 or 5 then it doesn't change
if 2nd dp is 1 to 3 then it becomes 0
if 2nd dp is 4 to 7 it becomes 5
if 2nd dp is 8 or 9 it will round up to 1st dp
I used an online translator for the formulas and so far so good, except where there ends in ,01 it seems, somehow the formula DOUBLES the total amount?

Prijswijziging_Berdy_01.01.22.xlsx
ABCD
1Purchase PriceExtra column for calculating pricesSales PriceNew Formula
2
37,1011,4511,9511,95
46,3510,2410,7410,75
519,5231,503232
68,4613,6514,1514,15
730,0748,5149,0198,01
810,7717,3817,8817,9
96,6410,7111,2122,41
100,000,000,50,5
118,7814,1714,6714,65
1224,4139,3739,8739,85
1396,98156,45156,95156,95
146,5110,501111
1522,1335,7036,2036,20
16107,06172,73173,23173,25
177,3211,8112,3124,61
1827,8944,9945,4945,5
195,398,709,209,20
2012,3319,8920,3920,4
Blad1
Cell Formulas
RangeFormula
A3:A20A3=J3*0.75
D3:D20D3=IF(OR(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=5),C3,IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<3),C3--TRUNC(C3,1),IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>=3,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<=7),C3+(0.05-(C3-TRUNC(C3,1))),ROUND(C3,1))))
 
Upvote 0
=IF(OR(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))=5),C3,IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>0,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<3),C3--TRUNC(C3,1),IF(AND(VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))>=3,VALUE(RIGHT(ROUND((C3-(TRUNC(C3,1))),2),1))<=7),C3+(0.05-(C3-TRUNC(C3,1))),ROUND(C3,1))))

the issue is the blue bit above change it to

C3-(c3-trunc(c3,1))
 
Upvote 0
Solution

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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