I wrote this VBA function for Excel but it doesn't work. Can someone help me to find out the error?
Thank's a lot!
Thank's a lot!
Code:
Option Explicit
Option Base 1
Function HPJ(data As Range, lambda As Double) As Variant
Dim nobs As Long
Dim i As Long, k As Long
Dim HPout() As Variant
Dim K1() As Variant, K2() As Variant, K3() As Variant, K4() As Variant, K5() As Variant, K6() As Variant
nobs = data.Rows.Count
ReDim HPout(1 To nobs)
For k = 1 To nobs Step 1
HPout(k) = data(k)
Next k
ReDim K1(nobs - 2, nobs)
For i = 1 To nobs - 2 Step 1
For k = 1 To nobs Step 1
K1(i, k) = 0
If i = k Then K1(i, k) = 1
If i = k + 2 Then K1(i, k) = 1
If i = k + 1 Then K1(i, k) = -2
Next k
Next i
ReDim K2(nobs, nobs - 2)
For i = 1 To nobs - 2 Step 1
For k = 1 To nobs Step 1
K2(k, i) = K1(i, k)
Next k
Next i
ReDim K3(nobs, nobs)
For i = 1 To nobs Step 1
For k = 1 To nobs Step 1
K3(i, k) = 0
If i = k Then K3(i, k) = 1
Next k
Next i
ReDim K4(1 To nobs)
ReDim K5(nobs, nobs)
ReDim K6(nobs, nobs)
K5 = Application.WorksheetFunction.MMult(K2, K1)
For i = i To nobs Step 1
For k = 1 To nobs Step 1
K5(i, k) = lambda * K5(i, k)
Next k
Next i
For i = i To nobs Step 1
For k = 1 To nobs Step 1
K6(i, k) = K5(i, k) + K3(i, k)
Next k
Next i
K4 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MInverse(K6), HPout)
HPJ = K4
End Function