VBA Code to check values and allow only 2 decimals

a984sce

New Member
Joined
Jun 23, 2017
Messages
2
All,
Please can I request your help/assistance on the following.

I have a workbook with 9 different worksheets which are named. Example Summary, FT Employee, PT Employee etc.
In each of the worksheet the resulting calculated column should contain values with only 2 decimal places. For example 15.12 is correct, but 15.123 is dis-allowed and any rounding Up or Down to 2 decimals is not allowed.
The columns with resulting values would be different column no., so we need to define this column no in the VBA Code.

Please can you share the VBA Code which will perform the check and correct the values with decimals.

Thank you.

regards
venkat
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the forum...

The worksheet function TRUNC should accomplish what you're looking for.

Excel 2010
AB
1NumberTruncated #
215.12315.12
315.29915.29
415.74915.74
Sheet2
Cell Formulas
RangeFormula
B2=TRUNC(A2,2)


A VBA approach:
Code:
Sub Test()

Dim rng As Range
Dim cel As Range


Set rng = Range([A2], Cells(Rows.Count, 1).End(xlUp))
For Each cel In rng.Cells
    cel = TruncTo(cel.Value, 2)
Next cel
End Sub


Public Function TruncTo(dblValue As Double, lngPlaces As Long) As Double
TruncTo = Int(dblValue * 10 ^ lngPlaces) / 10 ^ lngPlaces
End Function
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,458
Members
449,161
Latest member
NHOJ

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