Reputation: 17
I'm using worksheetFunction.pmt and need the output in the msgbox to be rounded to 2 decimal places or to display just the whole number.
I have declared the variables as Double and tried to add Round into the msgbox output but get a runtime error 13. I can't figure out where to put the Round function
Sub pmt()
Dim Sum, Rate, Period As Double
Sum = InputBox("Enter the sum of the loan")
Rate = InputBox("Enter the interest rate")
Period = InputBox("Enter the number of payments")
MsgBox "Your PMT is: $ " & WorksheetFunction.pmt(Rate, Period, -Sum) & ""
End Sub
I expect the result to return only 2 decimal places. Is it possible to round the worksheetFunction.pmt
Upvotes: 1
Views: 183
Reputation: 5848
Just use the VBA Round
function:
Sub pmt()
'Take care when naming your variables
'Dim dlbSum as Double, dblRate as Double, dblPeriod as Double
'Defining Period as Dboule doesn't make sense anyway
Dim Sum, Rate, Period As Double
Sum = InputBox("Enter the sum of the loan")
'You should consider error checking, entering the percentage symbol will create an error in the calculations
Rate = InputBox("Enter the interest rate")
Period = InputBox("Enter the number of payments")
MsgBox "Your PMT is: $ " & Round(WorksheetFunction.pmt(Rate, Period, -Sum), 2) & ""
End Sub
Upvotes: 1