VBA Rounding Problem

Sunjinsak

Board Regular
Joined
Jul 13, 2011
Messages
151
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have a problem with the way results of a calculation are displayed on a user form I’ve made in VBA for Excel 2003.<o:p></o:p>
<o:p></o:p>
I have two calculations which work out two results – resultA and resultB.<o:p></o:p>
<o:p></o:p>
There are no issues with the calculations themselves as they are working perfectly and calculating the correct result every time.<o:p></o:p>
<o:p></o:p>
The problem comes when I try to display those results on the user form. They have to be displayed in a very specific way when displayed to the end user (the reason for this is too long to go into here but suffice to say there is no way around it unfortunately).<o:p></o:p>
<o:p></o:p>
resultA always needs to be rounded up to the nearest integer. For example a result of 177.6 should be displayed as 178 - as would a result of 177.1.<o:p></o:p>
<o:p></o:p>
resultB should not be rounded at all (in either direction) but displayed to one decimal place. For example a result of 62.16 should be displayed as 62.1. A result of 59.52 should be displayed as 59.5 etc<o:p></o:p>
<o:p></o:p>
I’ve written the following code to display the results:<o:p></o:p>
<o:p></o:p>
Code:
LabelA.Caption = Application.RoundUp(resultA, 0)
<o:p></o:p>
Code:
LabelB.Caption = Format(resultB, ".#")
<o:p></o:p>

<o:p></o:p>
Unfortunately, using the figures given earlier as examples (177.6 for resultA and 62.16 for resultB), the above code displays the results as 180 and 63. They should however be displayed as 178 and 62.1 respectively.<o:p></o:p>
<o:p></o:p>
Can anyone point out where I’m going wrong or otherwise help me achieve what I want to do?<o:p></o:p>
<o:p></o:p>
Thanks.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
For result B try
Code:
Application.Floor(resultB, 0.1)

That, and your existing ResultA worked for me, with the variables declared as double.
 
Upvote 0
You can use the opposite of FLOOR for result A like:
Code:
LabelA.Caption = Application.Ceiling(resultA,1)
 
Upvote 0
Guys thank you both so much.

This has been driving me crazy for ages and now it's fixed.

Greatly appreciated.

Keep up the good work! :)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
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