I am trying to write UDF in VBA that will give me a the y-coordinates to confidence bands for a linear regression of data. I am new to programming with VBA but I have scoured the internet in order to try and figure out what I'm doing wrong, but I cannot figure it out. I basically can't get the loop to save each iteration as a value in an array that I can then output as the function answer. I can get it to output an array (Just spit back the range of x-values I put in), but if I try to save each iteration to a place in a new array it just gives me the #VALUE! error in each cell in my worksheet I've chosen for output. I am using Excel 2010
my code is here:
Function CINTERVAL(xvalues As Variant, yvalues As Variant, t)
'Pre-define variables
Dim n As Integer
Dim syx As Variant
Dim average As Variant
Dim ssx As Variant
Dim i As Integer
Dim x As Variant
'Perform Calculations
syx = WorksheetFunction.StEyx(yvalues, xvalues)
average = WorksheetFunction.average(xvalues)
ssx = WorksheetFunction.DevSq(xvalues)
n = WorksheetFunction.Count(xvalues)
x = xvalues.Value
'Store Values in an Array
For i = 1 To UBound(x)
CI.Value(i) = t * syx * (1 / n + (WorksheetFunction.Index(x, i) - average) ^ 2 / ssx ^ 2) ^ (1 / 2)
Next i
'Output Array
CINTERVAL = CI.Value
End Function
If anyone knows what I'm doing wrong i'd be soooo appreciative.
my code is here:
Function CINTERVAL(xvalues As Variant, yvalues As Variant, t)
'Pre-define variables
Dim n As Integer
Dim syx As Variant
Dim average As Variant
Dim ssx As Variant
Dim i As Integer
Dim x As Variant
'Perform Calculations
syx = WorksheetFunction.StEyx(yvalues, xvalues)
average = WorksheetFunction.average(xvalues)
ssx = WorksheetFunction.DevSq(xvalues)
n = WorksheetFunction.Count(xvalues)
x = xvalues.Value
'Store Values in an Array
For i = 1 To UBound(x)
CI.Value(i) = t * syx * (1 / n + (WorksheetFunction.Index(x, i) - average) ^ 2 / ssx ^ 2) ^ (1 / 2)
Next i
'Output Array
CINTERVAL = CI.Value
End Function
If anyone knows what I'm doing wrong i'd be soooo appreciative.