Rounding macro that doesn't change the cell contents (universal)

zorro6204

New Member
Joined
May 19, 2019
Messages
2
What I want is a Ctrl - o (say) marco, that takes an existing cell, whatever's in it, and adds "=round(" to the front and ",0)" to the back. If I try to record a macro, with or without relative references, when I call it again, it repeats the cell contents I was recording the macro in!

Example, if I record a macro in a cell that has +170+20 in it, I invoke record, then type:

F2 (turns on edit)
Home
=round(
Delete key (because stupid Excel replaces the + with an +)
End
,0)
Enter

And then back to stop macro recording. The result is "=round(170+20,0)". Good. But, if I then go to the next cell that has +200+100 in it, and call Ctrl-O, guess what I get back? Yup "=round(170+20,0)", the same contents as the previous cell. The cell contents of the cell I'm trying to round are wiped out.

That's bloody worthless of course. Any way around this? Believe it or not I'm just now moving from 123 to Excel. This was so easy in 123.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Re: How to: Rounding macro that doesn't change the cell contents (universal)

Welcome to the Forum. Try this.

Code:
Sub RoundIt()
ActiveCell.Value = Round(Evaluate(ActiveCell.Value), 0)
End Sub
 
Upvote 0
Re: How to: Rounding macro that doesn't change the cell contents (universal)

Also, assuming that you want to do it all in one fell swoop as opposed to one by one, the code below will do it. It assumes that your data is in column A starting in A1.

Code:
Sub AAO()
With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    .Value = Evaluate(Replace("IF({1},REPLACE(@,1,1,""=""))", "@", .Address))
    .Value = Evaluate(Replace("IF({1},ROUND(@,0))", "@", .Address))
End With
End Sub
 
Upvote 0
Re: How to: Rounding macro that doesn't change the cell contents (universal)

Okay, I'll try it, thanks.
 
Upvote 0
Re: How to: Rounding macro that doesn't change the cell contents (universal)

Hi, if you want to preserve the formulas, here is another option you can try:

Code:
Sub addroundfx()
Dim c As Range
For Each c In Selection
    If c.HasFormula Then c.Formula = Replace(c.Formula, "=", "=round(", 1, 1, vbTextCompare) & ",0)"
Next c
End Sub

Select the cells you want to change and run the macro.
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,247
Members
448,879
Latest member
oksanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top