2015年3月7日 星期六

Excel VBA Round Function and custom Round Function

This tutorial explains difference between Excel VBA Round Function and Excel worksheet Round Function, and create a custom Excel Round Function.


Excel VBA Round Function and custom Round Function


Excel VBA Round Function is completely different from Excel worksheet Round Function.


For Excel spreadsheet Round Function, if decimal part is >=0.5, the integer rounds up, otherwise the integer rounds down.


For Excel VBA Round Function, as well as Access VBA, the Round Function uses “Round to even” logic. Integer rounds up if decimal part >=0.6, round down if <=0.4. For case decimal part exactly =0.5,  then round down, otherwise round up(round up to even integer), same logic applies to negative number (See example).


I summarize the Excel VBA round to even logic below.


If decimal part >=0.6 Then
   Round up
ElseIf decimal part <=0.4 Then
   Round down
ElseIf decimal part = 0.5 Then
   If integer part is even number Then
        Round down
   ElseIf integer part is odd number Then
        Round up
  End If
End If

Round to even logic has other names:


-unbiased rounding


-convergent rounding


-statistician’s rounding


-Dutch rounding


-Gaussian rounding


-odd-even rounding


-bankers’ rounding


-broken rounding


Syntax of Excel VBA Round Function – Round to even logic


Round ( expression, [ decimal_places ] )

If [decimal_places] is omitted, default is 0 (round to integer)


Example of Excel VBA Round Function – Round to even logic


Round([Number],0)
















































































































































































NumberVBA Roundspreadsheet RoundCheck Diff
0.100
0.200
0.300
0.400
0.501integer part is even number, round down
0.611
0.711
0.811
0.911
111
1.111
1.211
1.311
1.411
1.522integer part is odd number, round up
1.622
1.722
1.822
1.922
222
2.122
2.222
2.322
2.422
2.523integer part is even number, round down
2.633
2.733
2.833
2.933
333
3.133
3.233
3.333
3.433
3.544integer part is odd number, round up
3.644
3.744
3.844
3.944
444
4.144
4.244
4.344
4.444
-4.545integer part is even number, round down
-4.655
-4.755
-4.855
-4.955
-555
-5.155
-5.255
-5.355
-5.455
-5.566integer part is odd number, round up

Workaround to apply spreadsheet Round Function


In Excel VBA, you can apply Excel worksheet Round Function in the below two ways


Range("B1").Value = "=Round(A1,0)"   OR

Range("B1").Value = Application.WorksheetFunction.Round(Range("A1").Value, 0)

Alternatively, you can create a custom Excel Round Function.


VBA Code of custom Excel Round Function – simulate Excel spreadsheet Round Function


Some custom Round Function you can find on the Internet do not consider cases for negative number , but this one does.


Function wRound(pValue As Double, digit) As Double
    Dim ExpandedValue
    Dim IntPart As Long
    Dim FractionPart As Double
    Dim negNumber As Boolean
    If pValue < 0 Then
        pValue = 0 - pValue
        negNumber = True
    End If
    
    ExpandedValue = pValue * (10 ^ digit) 'Retrieve integer part of the number
    IntPart = Int(ExpandedValue)
    FractionPart = ExpandedValue - IntPart 'Retrieve the fraction part of the number
    If FractionPart < 0.5 Then
        result = IntPart / 10 ^ digit
    ElseIf FractionPart >= 0.5 Then
        result = (IntPart + 1) / 10 ^ digit
    End If
    
    If negNumber = True Then
        wRound = 0 - result
    Else
        wRound = result
    End If
End Function

Syntax of custom Excel VBA Round Function – simulate Excel Round function


wRound(pValue, digit)





pValuethe decimal value you want to convert
digitthe number of decimal places you want to convert to

 Outbound References


http://www.techonthenet.com/excel/formulas/round_vba.php


 



Excel VBA Round Function and custom Round Function

沒有留言:

張貼留言