ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Rounding Macro (https://www.excelbanter.com/excel-programming/341999-rounding-macro.html)

scott

Rounding Macro
 
Hello experts,

I am a beginner at writing VBA so I could really use some help.

Objective: I'm trying to write a program that will round the selected
numbers to 2 decimal places. I am well aware of the =ROUND command but I'd
like to simplify my work. Ideally, I'd like to highlight a range of one cell
or a range of many cells, select my macro button for "Round to two decimal
places," and then see all my selected numbers rounded to two decimal places.
My use of the =ROUND command is taking too much time.

My current script is giving me a circular reference error which I understand
from my code, I'm just not sure how to fix it. Additionally, my code is only
for rounding one cell whereas I'd also like to have my macro have the ability
to round a range of selected cells.

My current code with circular reference error: ActiveCell.FormulaR1C1 =
"=ROUND(R[0]C[0],2)"

Any suggestions? Thanks in advance for your help.

-Scott

Tom Ogilvy

Rounding Macro
 
Sub RoundToTwo()
dim cell as Range, sStr as String
for each cell in selection
if cell.HasFormula then
sStr = cell.Formula
sStr = "=round(" & Right(sStr,len(sStr)-1) & ",2)"
cell.formula = sStr
else
cell.value = Application.Round(cell.value,2)
end if
cell.Numberformat = "#,##0.00"
Next
End Sub
assumes no array formulas in the selection

--
Regards,
Tom Ogilvy

"Scott" wrote in message
...
Hello experts,

I am a beginner at writing VBA so I could really use some help.

Objective: I'm trying to write a program that will round the selected
numbers to 2 decimal places. I am well aware of the =ROUND command but I'd
like to simplify my work. Ideally, I'd like to highlight a range of one

cell
or a range of many cells, select my macro button for "Round to two decimal
places," and then see all my selected numbers rounded to two decimal

places.
My use of the =ROUND command is taking too much time.

My current script is giving me a circular reference error which I

understand
from my code, I'm just not sure how to fix it. Additionally, my code is

only
for rounding one cell whereas I'd also like to have my macro have the

ability
to round a range of selected cells.

My current code with circular reference error: ActiveCell.FormulaR1C1 =
"=ROUND(R[0]C[0],2)"

Any suggestions? Thanks in advance for your help.

-Scott





All times are GMT +1. The time now is 12:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com