Erreur #Valeur de mon fonction VBA

Résolu/Fermé
amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017 - 19 avril 2017 à 22:32
amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017 - 20 avril 2017 à 11:55
Bonsoir le forum,

je m'adresse à vous pour m'aider à résoudre ce soucis.En effet , j'ai une fonction vba permettant de faire l'interpolation cubique spline cependant , elle ne me retourne pas le résultat attendu.Elle me retourne #Valeur.Sachant que j'ai déjà recherché sur les forum , mais malhereusement j'ai pas eu la solution.

ceci le code de la fonction:

Function cubic_spline(input_column, output_column, x As Double) As Double

Dim period_count As Integer
Dim rate_count As Integer
period_count = periodcol.Rows.Count
rate_count = ratecol.Rows.Count
If period_count <> rate_count Then
    spline = "Error: Range count dos not match"
    GoTo endnow
End If
 
ReDim xin(period_count) As Single
ReDim yin(period_count) As Single
Dim c As Integer
For c = 1 To period_count
xin(c) = periodcol(c)
yin(c) = ratecol(c)
Next c
Dim n As Integer
Dim i, k As Integer
Dim p, qn, sig, un As Single
ReDim u(period_count - 1) As Single
ReDim yt(period_count) As Single
n = period_count
yt(1) = 0
u(1) = 0
For i = 2 To n - 1
    sig = (xin(i) - xin(i - 1)) / (xin(i + 1) - xin(i - 1))
    p = sig * yt(i - 1) + 2
    yt(i) = (sig - 1) / p
    u(i) = (yin(i + 1) - yin(i)) / (xin(i + 1) - xin(i)) - (yin(i) - yin(i - 1)) / (xin(i) - xin(i - 1))
    u(i) = (6 * u(i) / (xin(i + 1) - xin(i - 1)) - sig * u(i - 1)) / p
    
    Next i
    
qn = 0
un = 0
yt(n) = (un - qn * u(n - 1)) / (qn * yt(n - 1) + 1)
For k = n - 1 To 1 Step -1
    yt(k) = yt(k) * yt(k + 1) + u(k)
Next k
Dim klo, khi As Integer
Dim h, b, a As Single
klo = 1
khi = n
Do
k = khi - klo
If xin(k) > x Then
khi = k
Else
klo = k
End If
k = khi - klo
Loop While k > 1
h = xin(khi) - xin(klo)
a = (xin(khi) - x) / h
b = (x - xin(klo)) / h
y = a * yin(klo) + b * yin(khi) + ((a ^ 3 - a) * yt(klo) + (b ^ 3 - b) * yt(khi)) * (h ^ 2) / 6

spline = y
endnow:

End Function


A voir également:

1 réponse

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
19 avril 2017 à 22:38
Bonjour,

A aucun moment tu ne retourne de valeur ....

Ta fonction se nomme "cubic_spline" ... et tu as visiblement utilisé "spline" comme variable de retour... d'où le problème...

Par exemple :
spline = "Error: Range count dos not match"


--->>
cubic_spline = "Error: Range count dos not match"

1
amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017 1
19 avril 2017 à 22:55
Bonsoir,

merci pour votre remarque , je suis désolé à propos ca , j'ai pas fait attention . j'ai déjà corrigé comme vous m'indiquer mais malhereusement ca ne marche pas!
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650 > amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017
19 avril 2017 à 23:47
Tu as défini ta fonction as DOUBLE ...
Function cubic_spline(input_column, output_column, x As Double) As Double

... tu ne peux donc pas retourner du texte ....
0
amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017 1 > jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024
20 avril 2017 à 00:44
Et alors? je sais cela
0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650 > amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017
20 avril 2017 à 10:00
... ben tu vire le AS DOUBLE !

SI tu avais correctement copié la fonction que tu as trouvé sur le net (sans essayer d'y renomer des trucs...) tu aurais eu :
Function spline(periodcol As Range, ratecol As Range, x As Range)
Dim period_count As Integer
Dim rate_count As Integer
period_count = periodcol.Rows.Count
rate_count = ratecol.Rows.Count
If period_count <> rate_count Then
    spline = "Error: Range count dos not match"
    GoTo endnow
End If
 
ReDim xin(period_count) As Single
ReDim yin(period_count) As Single
Dim c As Integer
For c = 1 To period_count
xin(c) = periodcol(c)
yin(c) = ratecol(c)
Next c
Dim n As Integer
Dim i, k As Integer
Dim p, qn, sig, un As Single
ReDim u(period_count - 1) As Single
ReDim yt(period_count) As Single
n = period_count
yt(1) = 0
u(1) = 0
For i = 2 To n - 1
    sig = (xin(i) - xin(i - 1)) / (xin(i + 1) - xin(i - 1))
    p = sig * yt(i - 1) + 2
    yt(i) = (sig - 1) / p
    u(i) = (yin(i + 1) - yin(i)) / (xin(i + 1) - xin(i)) - (yin(i) - yin(i - 1)) / (xin(i) - xin(i - 1))
    u(i) = (6 * u(i) / (xin(i + 1) - xin(i - 1)) - sig * u(i - 1)) / p
   
    Next i
   
qn = 0
un = 0
yt(n) = (un - qn * u(n - 1)) / (qn * yt(n - 1) + 1)
For k = n - 1 To 1 Step -1
    yt(k) = yt(k) * yt(k + 1) + u(k)
Next k
Dim klo, khi As Integer
Dim h, b, a As Single
klo = 1
khi = n
Do
k = khi - klo
If xin(k) > x Then
khi = k
Else
klo = k
End If
k = khi - klo
Loop While k > 1
h = xin(khi) - xin(klo)
a = (xin(khi) - x) / h
b = (x - xin(klo)) / h
y = a * yin(klo) + b * yin(khi) + ((a ^ 3 - a) * yt(klo) + (b ^ 3 - b) * yt(khi)) * (h ^ 2) / 6

spline = y
endnow:
End Function
0
amiro2017 Messages postés 194 Date d'inscription mardi 28 février 2017 Statut Membre Dernière intervention 31 août 2017 1 > jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024
20 avril 2017 à 10:15
Bonjour jordane45,

merci pour votre réponse
la fonction marché cependant le résultat fournit est faux!!
0