Adding Round funciton to handful of numbers

J

Jamie

Hi,

I have a sheet filled with raw data. I need to round each number and was
wondering if there is a formula or macro I can run to accomplish this. All
the numbers were entered in by hand.

Basically, I would like to highlight each number I need rounded and then run
the formula/macro.

Let me know if this is possible.

Thanks
 
R

Rick Rothstein

I think you will need a macro to do this. How did you want your numbers
rounded (round up, round down, round to nearest interval, Banker's Rounding,
normal rounding, to a set number of decimal places possibly coupled with one
of the previous methods, some other way)?
 
J

JoeU2004

Jamie said:
Basically, I would like to highlight each number I need
rounded and then run the formula/macro.

If you want to use a macro, the following should suffice.


Sub doit()
Dim cell As Range
For Each cell In Selection
cell = WorksheetFunction.Round(cell, 2)
cell.NumberFormat = "0.00"
Next cell
End Sub


Some comments:

1. I added a line to change the format (cell.NumberFormat). Delete that if
you wish to retain the original format.

2. I use Worksheet.Function.Round instead of the VBA Round function because
the latter rounds different (so-called "banker's rounding"). For example,
if the cell value is 295.425, WorksheetFunction.Round(cell,2) results in
295.43, whereas Round(cell,2) results in 295.42.

3. If you are unfamiliar with using macros, do the following:

a. In an active worksheet, press alt+F11 to open the VB window.

b. In the VB window, click on Insert > Module. That should open the VB
Editor pane.

c. Copy and paste the macro text above into the VB Editor pane.

d. In the worksheet window, select the cells to be converted, either one
at a time or use ctrl+leftClick. Then press alt+F8, select the macro, and
click Run.
 
J

JoeU2004

p45cal said:
cll.Value = Round(cll.Value, 2)'for actual rounding

Caveat emptor: VBA Round() performs "banker's rounding", which is different
from Excel ROUND(). For example, with 295.425, VBA Round() results in
295.42, whereas Excel ROUND() results in 295.43.

OR:
cll.NumberFormat = "0.00" 'to see rounded versions

If that is all the Jamie wants to do, I think it would be simpler to do it
directly in the worksheet. Simply right-click, click Format Cells > Number
Number, and select the number of decimal places, which defaults to 2.


----- original message -----
 
J

Jamie

This works but there are two changes I would like to make to it.

Can I have it so that the original number remains in the "formula bar" but
the rounded number shows up in the cell? I would like to have it so that if
the number were 88,888 the macro would insert the formula =Round("88,888", -2)

Also, can the macro keep the same number formatting? The one you gave me
changes the formatting to general.

Thanks
 
J

Jamie

I would like the have my number normally rounded. For example, 88,888 would
round to 88,900. As if I were to have the following formula:
=Round("88888",-2).
 
J

Jamie

Yes, I would like to retain the unrounded value in the cell and only see the
rounded number? I'm not exactly sure what your macro is that you wrote below.
Can you write it so that I can copy and paste it in a Module.

What I would like to have is a cell that has 88,888 in it and after I run
the macro the cell formula will be =Round("88888",-2) and the I will see the
number 88,900. I would also like to make the macro so that I can highlight
any cell either by hitting shift or holding down ctrl and the macro will
apply to all selected cells.

Thanks
 
R

Rick Rothstein

Okay, the fact that you want to retain the original number and only show the
rounded value is different than I originally interpreted your response. I'm
thinking, as long as I understand what you want correctly, that you can just
use normal cell formatting. Select all the cells you want to round (whether
they current have values in them or not... think of the future
possibilities), click Format/Cells in the menu bar, select the Number tab on
the dialog box that comes up, select "Number" from the Category List and
choose the number of decimal places you want all your numbers rounded to,
then click OK. When you go back to your sheet, any numbers in those cells
you selected originally will now display with the number of decimal places
you picked, but the actual value in the cells will not be changed.
 
R

Rick Rothstein

While most people in Excel will just use WorksheetFunction.Round to avoid
the Banker's Rounding problem, there is a wholly VB solution... use the
Format function. For whatever reason, Microsoft implemented Banker's in
every VB function that rounds values (CInt, CLng, Mod, \ operator, etc.)
*except* for the Format function. So, if you want to do "normal" rounding,
use the Format function instead of the Round function...

MsgBox Format(295.425, "0.00")
 
J

JoeU2004

Rick Rothstein said:
you can just use normal cell formatting.

That's what I thought, too; but I'm drawing a blank. What numeric format
displays the equivalent of ROUND(...,-2) -- i.e. rounds to hundreds?

Okay, the fact that you want to retain the original number and only show
the rounded value is different than I originally interpreted your
response.

Yeah, Jamie is feeding his/her requirements to us in pieces. See the
response to "p45cal". Specifically, Jamie writes: "I would also like to
make the macro so that I can highlight any cell either by hitting shift or
holding down ctrl and the macro will apply to all selected cells".

Sounds like Jamie is hoping for some kind of event macro. Any ideas?

I would opt for a "button" (control) that invokes the macro after making the
cell selection.

But then again, I do not trust my interpretation of Jamie's requirements.
Personally, I would still opt for a formatting solution, unless I am
crafting the worksheet for others to use.


----- original message -----
 
R

Rick Rothstein

Okay, I think I'm up with what she wants now. I missed the...

ROUND(88888,-2) ==> 88,900

example in the OP's response to p45cal and simply thought the -2 was a typo
in the OP's response to me.

To Jamie: I don't think you can do what you want with formatting or with a
macro. There is no format for showing a number in hundreds and a macro
cannot make one up. To show a number in the format you want, that number
*must* be modified (divided by 100) and that would break your other
condition to show the original number in the cell. Excel can show numbers in
thousands (and millions, billions) and still keep the original number in the
cell, but not when the rounding is not a multiple of 3.
 
J

Jamie

Sorry for the confusion. Let me clarify myself further. I have a sheet with
hard coded numbers. There are certain numbers within the sheet that I would
like to round to the nearest hundreth (ex: 725 = 700, 888 = 900, 1,456 =
1,500 etc.).

I want to create a macro so that the original number in the formula bar
remains visible, however the number in the cell is rounded. Essentially, I
want a macro that take my formula bar number, 725, and inserts the round
function so that I now have a formula that looks like this: =Round("725",-2).
This will make the number that appears in the cell display the value of 700.

Earlier, JoeU2004 gave the macro below:

Sub doit()
Dim cell As Range
For Each cell In Selection
cell = WorksheetFunction.Round(cell, 2)
cell.NumberFormat = "0.00"
Next cell
End Sub

This worked, however it changed the number in the formula bar, which I do
not want to have happen. It also rounded to the wrong place, but I was able
to fix that since I at least know that much. If there is more clarification
please let me know.

Thanks,

Jamie
 
R

Rick Rothstein

Does this do what you want?

Sub RoundToHundreds()
Dim C As Range
For Each C In Selection
C.Formula = "=ROUND(" & C.Value & ", -2)"
Next
End Sub
 
J

Jamie

Take a look at the below Macro someone wrote for me a little while back:

Sub addround()
Dim cell As Range
For Each cell In Selection
If cell.HasFormula And IsNumeric(cell) Then
cell = "=ROUND(" & Mid(cell.Formula, 2, 1000) & ",-2)"
End If
Next cell
End Sub

This macro does exactly what I need but enters the =Round() formula around a
formula I already have in the cell. For example if I had the formula =A25*G55
in a cell the above macro would change that cell formula to
=Round(A25*G55,-2).

Does that help anyone come up with macro so solve the problem of this post?
 
R

Rick Rothstein

Are you *sure* that JoeU2004's macro is *really* it? As far as I can tell,
his code physically changes the original value in the cell to the rounded
value (thereby losing your original value).
 
R

Rick Rothstein

This macro does exactly what I need but enters the =Round() formula around
a
formula I already have in the cell. For example if I had the formula
=A25*G55
in a cell the above macro would change that cell formula to
=Round(A25*G55,-2).

Okay, I'm confused again. Your original post said your cells had "raw data"
which I did not take to mean there were formulas in the cell. Now you are
mentioning formulas whereas each of your previous postings showed constant
values. And you say the code you just posted does what you want, but
obviously it doesn't (your example does not clarify what you are actually
looking for) or you wouldn't have posted in the first place. Instead of
adding new information with each post, do the following... show us a sample
of what is in a cell now... that is, what is in the Formula Bar and what is
displayed in the cell, then show us exactly what you want to be showing in
the Formula Bar and cell *after* the macro has run.
 
J

JoeU2004

Rick Rothstein said:
Are you *sure* that JoeU2004's macro is *really* it?

I concur. In the final analysis, following Jamie's "twisty maze" of
ever-changing requirements, it seems that what does the job is a macro that
replaces the number in a cell with a formula of the form =ROUND(number,-2).
You provided that macro elsewhere in this "thread". ("Web" would be a
better description).


----- original message -----
 
R

Rick Rothstein

There is no question I am confused as to what Jamie is actually looking for.
The only problem I know see with either of our macros is Jamie's latest
mention of (if I interpreted the post correctly) preserving formulas in
cells.
 
J

JoeU2004

Rick Rothstein said:
Okay, I'm confused again.

You're fine. Take a deep breath. I think you are getting confused by
postings that are crossing on the Internet, as well as other factors that I
will not mention to be polite ;).

In the posting timestamped 12:14p, Jamie explained exactly what is needed,
to wit: given a cell with a constant, change it to a formula of the form
=ROUND(constant,-2).

In the posting timestamped 12:23p, you provided exactly the right solution.
Problem solved. End of thread. Whew!

But to explain the confusion....

In the posting timestamped 12:27p, Jamie responded to your 12:10p response
to me in which you acknowledged that formatting alone would not solve the
problem.

Jamie then tried to explain __kind__ of solution he/she is expecting by
showing a solution to a __previous__ problem.

In the previous problem, someone had a formula in the cell, not a constant,
so of course the solution used MID(...,2,...) to strip off the formula's "="
to form the new formula, =ROUND(formula,-2).

Jamie misspoke when he/she wrote that the previous formula "does exactly
what I need". Of course, that formula does __not__ work when there is a
constant in the cell because there is no "=" to strip off. That is why
Jamie was seeking a solution to "this posting" (i.e. the topic of this
thread).

Arguably, the simplest solution is to change MID(...,2,...) to
MID(...,1,...). But of course, Rick's solution is more straight-forward for
that case.

Rick, I hope I have resolved your confusion.

Jamie. I think it would be sufficient for you to acknowledge that Rick's
posting timestamped 12:23p is all you need, and the problem is solved.


----- original message -----
 

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