Rounding a range of cells in VBA

daveus

Board Regular
Joined
May 18, 2004
Messages
191
I searched and couldn't make anything work that i found on rounding a range of cells. I have a range selected:

Range(Cells(therow, thecolumn), Cells(RowEnd, thecolumn + 2)).Select

I changed the number format like this:

Selection.NumberFormat = "0.00"

However, when i write to a text file, all the decimal places come back. How can i round the selected cells to the above "0.00" format in VBA. Thanks all!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Before you write to a text file, try copying the cell(s) to someplace else and PasteSpecial them with Values and Formats. Does that do it?
 
Upvote 0
I'm not sure how to do that...I am actually pasting the range from another location, so if I can paste them with format, etc. in VBA, that would be great. If you would elaborate, i would appreciate it. Thanks!
 
Upvote 0
Instead of using .Paste, use .PasteSpecial

There's more in the VBA help under "PasteSpecial Method (Range Object)". Here's an example from the forum, too.
 
Upvote 0
Formatting only affects the look of the value. Its underlying true value remains unchanged and that is why your text file is doing what it is doing.

After this line:
Range(Cells(therow, thecolumn), Cells(RowEnd, thecolumn + 2)).Select


Add this line:
Selection.Value = Selection.Text


Edit - - the code line assumes you already rounded the values earlier in your macro as you implied.
 
Upvote 0
Formatting will only affect how the number is displayed. Take a look at the Round function in VBA, eg
Code:
Sub rounding()

Dim LCol As Long
Dim ColNum As Long
Dim rng As Range
Dim cel As Range

LCol = ActiveSheet.UsedRange.Column - 1 + _
        ActiveSheet.UsedRange.Columns.Count

For ColNum = 1 To LCol
On Error Resume Next
   Set rng = Intersect(ActiveSheet.UsedRange, Columns(ColNum), _
       Cells.SpecialCells(xlCellTypeConstants, 23))
   For Each cel In rng
       cel.Value = Round(cel.Value, 2)
    Next cel
Next ColNum

End Sub
 
Upvote 0
Rounding Function in VBA

Does this rounding funtion work in Excel '97?

I used this code and it worked in 2000, but I received an error using '97.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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