Vba help!

tennisplayer

New Member
Joined
Dec 4, 2010
Messages
11
I'm currently trying to create a rounding code in VBA.

How do I create a rounding code that allows all values in one column in the excel spreadsheet to be rounded to a specific number of places?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You could put something like this in Worksheet_Change. This will round all entries in Column D to 2 decimal places:

Code:
    If Target.Column = 4 Then
        Target.Value = Round(Target.Value, 2)
    End If
 
Upvote 0
Possibly this? (Test in a copy of your workbook)

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Round_Column()<br>    <SPAN style="color:#00007F">Const</SPAN> Col <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "F" <SPAN style="color:#007F00">'<-- Column to round</SPAN><br>    <SPAN style="color:#00007F">Const</SPAN> Dec <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 3    <SPAN style="color:#007F00">'<-- Decimal places to round to</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    Columns(Col).Offset(, 1).Insert<br>    <SPAN style="color:#00007F">With</SPAN> Range(Col & 1, Range(Col & Rows.Count).End(xlUp))<br>        .Offset(, 1).FormulaR1C1 = "=IF(ISNUMBER(RC[-1])," _<br>            & "ROUND(RC[-1]," & Dec & "),IF(RC[-1]="""","""",RC[-1]))"<br>        .Value = .Offset(, 1).Value<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Columns(Col).Offset(, 1).Delete<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,704
Messages
6,126,321
Members
449,308
Latest member
Ronaldj

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