Rounding Macro

G

Guest

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
 
T

Tom Ogilvy

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
 

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

Top