Automatic rounding down number at cell entering

Hidden Dan

Board Regular
Joined
Dec 7, 2016
Messages
63
In a financial spreadsheet I have some ranges where user can type in financial values with 2 decimal places. But I want them all be automaticly rounded down to zero decimals. This should happen after the value has been typed in.

Examples
1,250.23 --> 1,250
1,250.81 --> 1,250


Is there a VBA script that can perform such a task ?


Thanks, Dan
 

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
If these values are being entered manually one-by-one, you can use a Worksheet_Change event procedure to do this, which runs automatically as they are entering data.
Simply right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 And IsNumeric(Target) Then
        If Target.Column = 4 Then
            Application.EnableEvents = False
            Target = Int(Target)
            Application.EnableEvents = True
        End If
    End If
End Sub
This example is set to work for column D. Just change this line for any other column:
Code:
        If Target.Column = 4 Then
Note that "A"=1, "B"=2, "C"=3, "D"=4, etc.
 
Upvote 0
Thans Joe,

I made a simple try and it works. Smart how value is truncated to the integer value.

May I ask you 1 additional question ? How would I make this script running for 1 named range in a sheet instead of a Column ?


Thanks, Dan
 
Upvote 0
Use this variation (in this example, my range is named "MyRange"):
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng As Range
    Dim cell As Range
    
'   See if updated cell in range named "MyRange"
    Set rng = Intersect(Target, Range("MyRange"))
    If Not rng Is Nothing Then
        For Each cell In rng
            Application.EnableEvents = False
            cell = Int(cell)
            Application.EnableEvents = True
        Next cell
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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