VBA conversion txt en number
Fermé
bambi2929
Messages postés
4
Date d'inscription
vendredi 28 mars 2014
Statut
Membre
Dernière intervention
6 avril 2014
-
28 mars 2014 à 23:11
bambi2929 Messages postés 4 Date d'inscription vendredi 28 mars 2014 Statut Membre Dernière intervention 6 avril 2014 - 6 avril 2014 à 11:48
bambi2929 Messages postés 4 Date d'inscription vendredi 28 mars 2014 Statut Membre Dernière intervention 6 avril 2014 - 6 avril 2014 à 11:48
A voir également:
- VBA conversion txt en number
- Monnaie conversion - Télécharger - Banque & Budget
- Vba ouvrir un fichier excel avec chemin ✓ - Forum VB / VBA
- Find vba - Astuces et Solutions
- Conversion mb en go ✓ - Forum Mail
- Vba attendre 1 seconde ✓ - Forum VB / VBA
6 réponses
eriiic
Messages postés
24603
Date d'inscription
mardi 11 septembre 2007
Statut
Contributeur
Dernière intervention
15 décembre 2024
7 254
29 mars 2014 à 07:24
29 mars 2014 à 07:24
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
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
bambi2929
Messages postés
4
Date d'inscription
vendredi 28 mars 2014
Statut
Membre
Dernière intervention
6 avril 2014
29 mars 2014 à 15:01
29 mars 2014 à 15:01
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
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
eriiic
Messages postés
24603
Date d'inscription
mardi 11 septembre 2007
Statut
Contributeur
Dernière intervention
15 décembre 2024
7 254
Modifié par eriiic le 29/03/2014 à 17:03
Modifié par eriiic le 29/03/2014 à 17:03
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
edit : https://www.cjoint.com/?DCDrduIytYN
JvDo
Messages postés
1978
Date d'inscription
mercredi 27 juillet 2005
Statut
Membre
Dernière intervention
28 septembre 2020
858
29 mars 2014 à 19:13
29 mars 2014 à 19:13
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
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
bambi2929
Messages postés
4
Date d'inscription
vendredi 28 mars 2014
Statut
Membre
Dernière intervention
6 avril 2014
30 mars 2014 à 19:12
30 mars 2014 à 19:12
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.
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.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
cs_Le Pivert
Messages postés
7904
Date d'inscription
jeudi 13 septembre 2007
Statut
Contributeur
Dernière intervention
14 août 2024
729
31 mars 2014 à 09:25
31 mars 2014 à 09:25
Bonjour,
Regarde ceci, il y a un classeur à télécharger qui fait exactement ce que tu veux:
https://didier-gonard.developpez.com/tutoriels/office/excel/obliger-saisie-numerique-dans-texbox/
Regarde ceci, il y a un classeur à télécharger qui fait exactement ce que tu veux:
https://didier-gonard.developpez.com/tutoriels/office/excel/obliger-saisie-numerique-dans-texbox/
bambi2929
Messages postés
4
Date d'inscription
vendredi 28 mars 2014
Statut
Membre
Dernière intervention
6 avril 2014
6 avril 2014 à 11:48
6 avril 2014 à 11:48
Merci à tous pour votre aide.
Bizarrement, Cdbl() fonctionne maintenant.
Encore une fois merci.
Bizarrement, Cdbl() fonctionne maintenant.
Encore une fois merci.