Schwirigkeiten mit dynamischen Array
24.07.2017 16:02:55
Sebastian
ich habe meine Schwirigkeiten mit einem dynamischen Array, welches ich durch for und if-schleifen produzieren möchte. Der VBA-Code lautet wir folgt und scheitert an ReDim auf Grund von Preserve (Datenfehler bereits dimensioniert). Wenn ich preserve entferne bekomme ich ein "Laufzeitfehler 13" weil theLeastSquareCoef fehler 2015 aufweißt bzw. Array y1 und x1 meiner Meinung nach nicht deklariert sind, wie ich es möchte. Ich würde mich sehr über eure Hilfe freuen. Ach, noch ein Hinweis, der hier aufgeführte Code ist ein Auszug.
Dim i, n As Integer
n = 1
Dim x(1 To 7, 1 To 1)
x(1, 1) = 7
x(2, 1) = 8
x(3, 1) = 9
x(4, 1) = 10
x(5, 1) = 12
x(6, 1) = 14
x(7, 1) = 16
Dim y1(1 To 7, 1 To 1) As Variant
Dim x1(1 To 7, 1 To 1) As Variant
y1(1, 1) = y(1, 1)
x1(1, 1) = x(1, 1)
For i = 2 To 7
Stop
If y(i, 1) "NDM" Then
If y(i, 1) = 1 And y(i - 1, 1) = 1 Then
n = n - 1
ReDim Preserve y1(1 To n, 1 To 1) As Variant
ReDim Preserve x1(1 To n, 1 To 1) As Variant
y1(n, 1) = y(i, 1)
x1(n, 1) = x(i, 1)
Else
ReDim Preserve y1(1 To n, 1 To 1) As Variant
ReDim Preserve x1(1 To n, 1 To 1) As Variant
y1(n, 1) = y(i, 1)
x1(n, 1) = x(i, 1)
End If
If i 7 Then
n = n + 1
End If
End If
Next
'y1(1, 1) = y(3, 1)
'y1(2, 1) = y(4, 1)
'y1(3, 1) = y(5, 1)
'y1(4, 1) = y(6, 1)
'X1(1, 1) = x(3, 1)
'X1(2, 1) = x(4, 1)
'X1(3, 1) = x(5, 1)
'X1(4, 1) = x(6, 1)
Dim R2 As Double
theLeastSquareCoef = Application.LinEst(y1, Application.Power(x1, Array(1, 2, 3)), , True)
Alpha0_Mach_Effect = Application.Index(theLeastSquareCoef, 1, 1) * t_to_c ^ 3 + _
Application.Index(theLeastSquareCoef, 1, 2) * t_to_c ^ 2 + _
Application.Index(theLeastSquareCoef, 1, 3) * t_to_c + _
Application.Index(theLeastSquareCoef, 1, 4)
R2 = Application.Index(theLeastSquareCoef, 3, 1)