VBA conversion txt en number

Bonsoir tout le monde,

J'ai passé près de 2h sur la conversion en vain alors que l'implémentation de mon programme ne m'a pas pris beaucoup de temps.

Voici ma question :

Je fais certains calculs qui se retrouvent chacun dans le textbox correspondant.

Ils sont donc au format texte.

Cependant, je veux les copier dans des cellules au format nombre.

J'ai utilisé les fonctions cint, cdbl, clng mais cela ne fonctionne pas quand les chiffres sont négatifs.

Ils sont mis à 0 alors que je souhaite obtenir pour tous mes résultats, quel que soit leur signe, un arrondi à 2 décimales.

Quelqu'un a t il une idée.

Merci d'avance.

6 réponses

  1. Contributeur
    Bonjour,

    Aucune raison que ça ne fonctionne pas, maintenant sans fichier on ne sait pas ce que tu fais exactement.
    Les cellules étant des Double, utilise de préférence Cdbl(), ça évitera une double conversion.

    eric
    0
    1. J'ai déjà essayé avec cdbl() mais cela ne fonctionne pas.

      Voici mon code associé au userform

      Private Sub CmdCalc_Click()

      If OpBCall.Value = True Then
      Type_option = "CALL"
      Else
      Type_option = "PUT"
      End If

      S = Val(TxtPrice.Value)
      K = Val(TxtStrike.Value)
      r = Val(TxtRisk.Value)
      sigma = Val(TxtVolatility.Value)

      ' 'Greeks
      If OpBCall.Value = True Then
      If ChkDelta.Value = True Then
      TxtDelta.Value = Delta(Type_option, S, K, r, sigma, Duration())
      Range("B14").Value = CDbl(TxtDelta.Text)
      '
      End If

      If ChkGamma.Value = True Then
      TxtGamma.Value = Gamma(S, K, r, sigma, Duration())
      Range("B15").Value = CDbl(TxtGamma.Text)

      End If

      If ChkRho.Value = True Then
      TxtRho.Value = Rho(Type_option, S, K, r, sigma, Duration())
      Range("B18").Value = CDbl(TxtRho.Text)
      End If

      If ChkVega.Value = True Then
      TxtVega.Value = Vega(S, K, r, sigma, Duration())
      Range("B17").Value = CDbl(TxtVega.Text)
      End If

      If ChkTheta.Value = True Then
      TxtTheta.Value = Theta(Type_option, S, K, r, sigma, Duration())
      Range("B16").Value = CDbl(TxtTheta.Text)
      End If

      Else

      If ChkDelta.Value = True Then
      TxtDelta.Value = Delta(Type_option, S, K, r, sigma, Duration())
      Range("D14").Value = CDbl(TxtDelta.Text)
      End If

      If ChkGamma.Value = True Then
      TxtGamma.Value = Gamma(S, K, r, sigma, Duration())
      Range("D15").Value = CDbl(TxtGamma.Text)
      End If

      If ChkRho.Value = True Then
      TxtRho.Value = Rho(Type_option, S, K, r, sigma, Duration())
      Range("D18").Value = CDbl(TxtRho.Text)
      End If

      If ChkVega.Value = True Then
      TxtVega.Value = Vega(S, K, r, sigma, Duration())
      Range("D17").Value = CDbl(TxtVega.Text)
      End If

      If ChkTheta.Value = True Then
      TxtTheta.Value = Theta(Type_option, S, K, r, sigma, Duration())
      Range("D16").Value = CDbl(TxtTheta.Text)
      End If
      End If
      '

      '

      End Sub

      'DAY AND MONTH
      Private Sub UserForm_Initialize()
      For i = 1 To 31
      CmbDay.AddItem i
      If i <= 12 Then CmbMonth.AddItem i
      Next i

      End Sub

      '' CRR steps
      'Private Sub TxtStep_Change()
      'TxtStep.Value = SpbStep.Value
      'End Sub

      'B&S

      Private Sub CmdPrice1_Click()

      If OpBCall.Value = True Then
      Type_option = "CALL"
      Else
      Type_option = "PUT"
      End If

      S = Val(TxtPrice.Value)
      K = Val(TxtStrike.Value)
      r = Val(TxtRisk.Value)
      sigma = Val(TxtVolatility.Value)

      TxtPriceBandS.Value = Black_and_Scholes(Type_option, S, K, r, sigma, Duration())
      If Type_option = "CALL" Then
      Range("B13").Value = CDbl(TxtPriceBandS.Text)
      Else
      Range("D13").Value = CDbl(TxtPriceBandS.Text)
      End If

      End Sub

      ''CRR
      '
      Private Sub CmdPrice2_Click()

      If OpBCall.Value = True Then
      Type_option = "CALL"
      Else
      Type_option = "PUT"
      End If

      S = Val(TxtPrice.Value)
      K = Val(TxtStrike.Value)
      r = Val(TxtRisk.Value)
      sigma = Val(TxtVolatility.Value)

      step = Val(TxtStep.Value)

      If OpbEuro.Value = True Then
      TxtPriceBinom.Value = Binomial_E(Type_option, S, K, r, sigma, Duration(), step)
      Range("B24").Value = TxtPriceBinom.Text
      Range("B23").Value = TxtStep.Text

      ElseIf OpbUS.Value = True Then
      TxtPriceBinom.Value = Binomial_U(Type_option, S, K, r, sigma, Duration(), step)
      Range("C24").Value = TxtPriceBinom.Text
      Range("C23").Value = TxtStep.Text

      End If

      End Sub

      Et celui associé aux fonctions

      Option Base 0

      ' Black and Scholes Formula

      Function Black_and_Scholes(Type_option, S, K, r, sigma, T)

      d1 = (Log(S / K) + (r + 0.5 * sigma ^ 2) * T) / (sigma * Sqr(T))

      d2 = d1 - sigma * Sqr(T)

      c = choice(Type_option)

      Black_and_Scholes = c * (S * Normal(c * d1) - K * Exp(-r * T) * Normal(c * d2))

      End Function

      ' Normal distribution used in the Black and Scholes Formula

      Function Normal(d)
      Normal = Application.WorksheetFunction.NormSDist(d)
      End Function

      ' Choice of the Put or the Call

      Function choice(Type_option)

      If Type_option = "CALL" Then
      choice = 1

      ElseIf Type_option = "PUT" Then
      choice = -1

      End If

      End Function

      'Duration

      Function Duration()

      date_d = DateSerial(TxtYear, CmbMonth, CmbDay)

      Duration = (Date - date_d) / 365
      End Function

      ' GREEKS

      'Delta

      Function Delta(Type_option, S, K, r, sigma, T)

      d1 = (Log(S / K) + (r + sigma ^ 2) * T) / (sigma * Sqr(T))
      c = choice(Type_option)

      Delta = c * Exp(-r * T) * Normal(c * d1)

      End Function

      ' Gamma

      Function Gamma(S, K, r, sigma, T)

      d1 = (Log(S / K) + (r + sigma ^ 2) * T) / (sigma * Sqr(T))

      Gamma = (Normal(d1) * Exp(-r) * T) / (S * sigma * Sqr(T))
      End Function

      Sub TEST()
      Range("B13").Value = Gamma(76, 170, 0.05, 0.05, 2)
      MsgBox d1
      End Sub

      ' Rho

      Function Rho(Type_option, S, K, r, sigma, T)

      d2 = (Log(S / K) + (r - sigma ^ 2 / 2) * T) / (sigma * Sqr(T))

      c = choice(Type_option)

      Rho = c * (T * K * Exp(-r * T) * Normal(c * d2))

      End Function

      'Vega

      Function Vega(S, K, r, sigma, T)

      d1 = (Log(S / K) + (r + sigma ^ 2) * T) / (sigma * Sqr(T))

      Vega = S * Exp(-r * T) * Normal(d1) * Sqr(T)
      End Function

      'Theta

      Function Theta(Type_option, S, K, r, sigma, T)

      d1 = (Log(S / K) + (r + sigma ^ 2) * T) / (sigma * Sqr(T))

      d2 = d1 - sigma * Sqr(T)

      c = choice(Type_option)

      Theta = (-(S * (Normal(d1) * Exp(-r * T) * sigma)) / (2 * Sqr(T)) + _
      c * (-r) * S * Normal(d1) * Exp(-r * T)) + c * r * K * Exp(-r * T) * Normal(c * d2)
      End Function

      'Binomial Option Pricing Model

      Function Binomial_E(Type_option, S, K, r, sigma, T, N)

      ReDim Price(N + 1) As Double

      c = choice(Type_option)
      delta_bis = T / N

      u = Exp(sigma * Sqr(delta_bis))
      d = 1 / u
      Prob = (Exp(b * delta_bis) - d) / (u - d)

      For j = 0 To N
      Price(j) = Application.WorksheetFunction.Max(c * (S * u ^ j * d ^ (N - j) - K), 0)
      Next j

      For i = N - 1 To 0 Step -1
      For j = 0 To i

      Price(j) = Exp(-r * delta_bis) * (Prob * Price(j + 1) + (1 - Prob) * Price(j))

      Next j
      Next i

      Binomial_E = Price(0)

      End Function

      Function Binomial_U(Type_option, S, K, r, sigma, T, N)

      ReDim Price(N + 1)

      c = choice(Type_option)
      delta_bis = T / N

      u = Exp(sigma * Sqr(delta_bis))
      d = 1 / u
      Prob = (Exp(b * delta_bis) - d) / (u - d)

      For j = 0 To N
      Price(j) = Application.WorksheetFunction.Max(c * (S * u ^ j * d ^ (N - j) - K), 0)
      Next j

      For i = N - 1 To 0 Step -1
      For j = 0 To i

      Price(j) = Application.WorksheetFunction.Max(c * (S * u ^ j * d ^ (i - j) - K), _
      Exp(-r * delta_bis) * (Prob * Price(j + 1) + (1 - P) * Price(j)))

      Next j
      Next i

      Binomial_U = Price(0)

      End Function

      Merci beaucoup
      0
      1. Contributeur
        Tu crois que je vais passer 1/2 h à essayer de tout reconstruire (mal forcément) alors que tu as le fichier ?

        edit : https://www.cjoint.com/?DCDrduIytYN
        0
    2. Bonjour à tous,

      Je vois que tu utilises la fonction VAL() pour S, k, r, sigma.
      Es-tu en paramètres régionaux américains ou fais-tu une saisie avec le point décimal?
      Sinon, tu peux inclure un replace de la virgule décimale par un point dans le VAL.

      Cdlt
      0
      1. Bonjour,

        Oui j'utilise la fonction val() pour ces paramètres car, dans les textboxs, les décimaux ne peuvent être renseignés qu'avec des points et donc, cela extrait des valeurs numériques au lieu de caractères pour faire les calculs.

        N y a t il pas un moyen, pour les textboxs qui affichent les résultats de forcer la récupération en numérique??

        Merci beaucoup.
        0
        1. Merci à tous pour votre aide.

          Bizarrement, Cdbl() fonctionne maintenant.

          Encore une fois merci.
          0