Rounding

  • Thread starter Thread starter Tasmin
  • Start date Start date
T

Tasmin

How do you round to 1 decimal place in code?
I know how to put a round formula in a cell, but I have a
value and need to reference, but not display, the rounded
result of.

Thanks
 
Sub RoundTest()
Dim x
x = 1.2345
Debug.Print Round(x, 1)
End Sub

--
Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"
 
be aware that VBA's Round() method and XL's ROUND() function differ in
how they round numbers that have 5 as their terminal digit. XL's ROUND()
always rounds away from zero (i.e., arithmetic rounding) and VBA's Round
rounds to the nearest even number (i.e., "banker's rounding"):


x Round(x,1) =ROUND(x,1)
1.05 1.0 1.1
1.15 1.2 1.2
1.25 1.2 1.3
1.35 1.4 1.4
 
Also be aware that the VBA round function was only introduced in Excel
2000 (OP did not specify version), and does not support negative values
for numdecimalplaces. You can call the worksheet Round function via
Application.Round(x,1)

Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top