CA Aman Ghotra
CA Aman Ghotra

Reputation: 1

VBA function returns wrong decimal values

Both of the function at the bottom return wrong values.

Example: for employee portion of 142.22, the answer should be 165.64 , and not 165.39 as returned by the VBA function?

Similarly , for the second function, for 5048.94 - EmployerMPPportion , the answer should be 50999.41, whereas the answer is returning 51000.

I am aware I have used round function but it should round up to 2 decimal points only?

I just read in the forum that "BCD are not very space-efficient, but that's a trade-off you have to make for accuracy in this case", if this is true then why do Excel's built-in functions always return correct values? Are they not designed in VBA?

Function MPPemployer (EmployeeMPPportion As Long)
    Dim c As Double
    c = Round((EmployeeMPPportion / (8.5 / 100) * (9.9 / 100)), 2)
    MPPemployer = c
End Function

Function Grossmppsalary (EmployerMPPportion As Long)
    Dim g As Double
    g = Round((EmployerMPPportion / (9.9 / 100)), 2)
    Grossmppsalary = g
End Function

Upvotes: 0

Views: 1203

Answers (1)

PatricK
PatricK

Reputation: 6433

You should define what the Function returns and change the the Input type to Double for the parameters. The answer to why, is that the function had the input rounded to Long before the calculation.

Function MPPemployer(EmployeeMPPportion As Double) As Double
    Dim c As Double

    c = Round((EmployeeMPPportion / (8.5 / 100) * (9.9 / 100)), 2)

    MPPemployer = c
End Function

Function Grossmppsalary(EmployerMPPportion As Double) As Double
    Dim g As Double

    g = Round((EmployerMPPportion / (9.9 / 100)), 2)

    Grossmppsalary = g
End Function

Upvotes: 2

Related Questions