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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
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?
 

daveus

Board Regular
Joined
May 18, 2004
Messages
191
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!
 

Tazguy37

MrExcel MVP
Joined
May 28, 2004
Messages
4,237
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.
 

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,257

ADVERTISEMENT

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.
 

KenWright

Active Member
Joined
Jan 14, 2005
Messages
267
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
 

Robb

Board Regular
Joined
Feb 17, 2002
Messages
145
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.
 

Forum statistics

Threads
1,147,676
Messages
5,742,555
Members
423,737
Latest member
tom_xls

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
Top