Why the hell do I get an ByRef argument type mismatch?

Spurious

Active Member
Joined
Dec 14, 2010
Messages
439
Hello guys,

I got the following code:

Code:
Public Function YTM(FaceV, Coupon, Price, Matur, Freq As Double) As Double
Dim i As Long
Dim Yld, Diff As Double

Const Inc As Double = 0.00001
Const Iters As Long = 100000

Select Case FaceV * Coupon / Price
    Case Is > Coupon
        Yld = (FaceV * Coupon / Price)
        Do
            Yld = Yld + Inc
            i = i + 1
            Diff = Price - CurrP(FaceV, Coupon, Matur, Freq, Yld)
        Loop While i <= Iters And Diff < 0
    Case Is < Coupon
        Yld = (FaceV * Coupon / Price)
        Do
            Yld = Yld - Inc
            i = i + 1
            Diff = Price - CurrP(FaceV, Coupon, Matur, Freq, Yld)
        Loop While i <= Iters And Diff > 0
    Case Is = Coupon
        Yld = Coupon
End Select
MsgBox Round(Yld * 100, 4) & "% Yield - " & Yld
YTM = Yld
End Function

Code:
Public Function CurrP(FaceV, Coupon, Matur, Freq, YTM As Double) As Double
Dim i As Integer
Dim Price As Double

Freq = 1 / Freq
Price = (Matur - (Freq * Int(Matur / Freq))) * Coupon * FaceV

For i = 1 To Int(Matur * Freq)
    Price = Price + (Coupon * FaceV * Freq) / (1 + YTM) ^ i
Next i
Price = Price + (FaceV) / (1 + YTM) ^ Int(Matur * Freq)
CurrP = Price
End Function

When I want to run the first function, I get an ByRef argument type mismatch for Yld and I dont understand why.

Thanks for help in advance!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:

Code:
Public Function YTM(FaceV, Coupon, Price, Matur, Freq As Double) As Double
Dim i As Long
Dim Yld As Double, Diff As Double

Const Inc As Double = 0.00001
Const Iters As Long = 100000

Select Case FaceV * Coupon / Price
    Case Is > Coupon
        Yld = (FaceV * Coupon / Price)
        Do
            Yld = Yld + Inc
            i = i + 1
            Diff = Price - CurrP(FaceV, Coupon, Matur, Freq, Yld)
        Loop While i <= Iters And Diff < 0
    Case Is < Coupon
        Yld = (FaceV * Coupon / Price)
        Do
            Yld = Yld - Inc
            i = i + 1
            Diff = Price - CurrP(FaceV, Coupon, Matur, Freq, Yld)
        Loop While i <= Iters And Diff > 0
    Case Is = Coupon
        Yld = Coupon
End Select
MsgBox Round(Yld * 100, 4) & "% Yield - " & Yld
YTM = Yld
End Function

When you Dim variables, you have to explicitly state what type they are, otherwise they will be stored as a Variant, which can lead to unwanted errors.
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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