Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 577
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding up M Thompson Excel Discussion (Misc queries) 2 May 19th 10 12:13 AM
ROUNDING FORMULA =ROUND(B12/$B$10,1) ROUNDING TO HIGH SunshineinFt.Myers[_2_] Excel Worksheet Functions 7 March 5th 09 06:41 PM
I need a formula with rounding up & rounding down to the nearest . Tony Kay Excel Worksheet Functions 3 May 29th 07 11:13 PM
Rounding Bill Ridgeway New Users to Excel 2 November 21st 06 11:32 AM
Worksheet rounding vs VBA rounding Simon Cleal Excel Programming 4 September 2nd 05 01:50 AM


All times are GMT +1. The time now is 02:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"