Results 1 to 5 of 5

Thread: Using WEND in VBA

  1. #1
    Join Date
    Jan 2012
    Posts
    66

    Using WEND in VBA

    While Abs(kw - Price) > 0.00005
    Code:
    T3 = T1 + (Price - k1) * (T2 - T1) / (k2 - k1)
    T1 = T2
    k1 = k2
    T2 = T3
    k2 = (-Application.PV(T2, n, Cpn, 100) + Cpn) / (1 + t) ^ t
    - sz
    Wend
    Does it merely mean While End, sort of like End If?

  2. #2
    Join Date
    Jul 2011
    Posts
    640

    Re: Using WEND in VBA

    While...Wend is just one of the control flow structures. I personally prefer Do...Loop but there is nothing wrong with While...Wend. Check out this link on the different control flow structures. As a side note, it is a little unusual here because both kw & Price do not get changed in the loop. It appears it could cause an endless loop. Usually, one or both get change in the loop so that eventually kw-Price falls below .00005.

  3. #3
    Join Date
    Aug 2011
    Posts
    566

    Re: Using WEND in VBA

    Interesting point. The VBA I pasted here is from a larger UDF that calculates the yield on a bond. Here's the entire function. Perhaps this would explain the purpose of the
    loop:
    Code:
    Function B_Yield_ISMA(Sett_d As Date, Mat_D As Date, Cpn As Double, Price As
    Double) As Double
    'Calculates the yield of a coupon bearing bond (ISMA)
    Dim L As Double, t As Double, sz As Double, T1 As Double, T2 As Double,
    T3 As Double
    Dim k1 As Double, k2 As Double
    Dim n As Long
    L = Application.Days360(Sett_d, Mat_D) / 360
    n = Int(L)
    t = L - n
    sz = Cpn * (1 - t)
    T1 = Application.Rate(L, Cpn, -Price, 100)
    If IsError(T1) Then T2 = -1 Else k1 = (-Application.PV(T1, n, Cpn,
    100) + Cpn) / (1 + T1) ^ t - sz
    T2 = T1 + 0.00005
    k2 = (-Application.PV(T2, n, Cpn, 100) + Cpn) / (1 + T2) ^ t - sz
    While Abs(kw - Price) > 0.00005
    T3 = T1 + (Price - k1) * (T2 - T1) / (k2 - k1)
    T1 = T2
    k1 = k2
    T2 = T3
    k2 = (-Application.PV(T2, n, Cpn, 100) + Cpn) / (1 + t) ^ t
    - sz
    Wend
    B_Yield_ISMA = T2
    End Function

  4. #4
    Join Date
    Aug 2011
    Posts
    695

    Re: Using WEND in VBA

    It appears to me that kw could be a typo. Looking at the code, my guess is that it should be : While Abs(k2 - Price) > 0.00005. It appears to loop until k2 (Pv) calculation is reduced to a value close to Price. Since "2" is next to "w" on the keyboard, my guess is that it should be k2, and not kw. You may be right. I got the code in an email from a colleague and have not had a chance to test it.

  5. #5
    Join Date
    Aug 2011
    Posts
    540

    Re: Using WEND in VBA

    Wend The While statement repeats an action until a condition is true.
    • While condition
    • Stock
    • Wend

    If the condition is true, the actions specified in the procedure are performed. When the instruction Wend is reached, the procedure returns to the While statement and condition is again checked. If condition is always true, the process is repeated. If the condition is false, execution jumps to the first line of code following the statement Wend.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,254,449.19450 seconds with 16 queries