Erreur #Valeur de mon fonction VBA
Résolu
amiro2017
Messages postés
207
Statut
Membre
-
amiro2017 Messages postés 207 Statut Membre -
amiro2017 Messages postés 207 Statut Membre -
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:
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
1 réponse
-
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"
-
-
-
-
... 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 -
-