vba use linest to calculate polynomial coefficients and index to output

南笙酒味 提交于 2020-01-17 03:23:41

问题


I have two rows of data, fracture pressure and depth. I have to code in vba to generate the polynomial (quadratic for this case) equation and then output the coefficients to the worksheet. I am using Linest and Index. For this two rows of data, I don't know how many datasets I have because I need to delete some noisy data first (the definition of noisy data is randomly so the number of datasets vary each time), so I can't use something like "A17:A80" in the linest function. However, it looks like the worksheet function in vba can't work for arrays.

Dim Frac_x, Frac_y As Range
Dim X
        Set Frac_x = Range(Cells(17, 1), Cells(e - 1, 1))
        Set Frac_y = Range(Cells(17, 7), Cells(e - 1, 7))
        X= Application.WorksheetFunction.LinEst(Frac_y,Frac_x,{1,2})
        Cells(3, 8).Value = Application.WorksheetFunction.Index(X, 1, 1)
        Cells(4, 8).Value = Application.WorksheetFunction.Index(X, 1, 2)
        Cells(5, 8).Value = Application.WorksheetFunction.Index(X, 1, 3)

In this code, e is defined in the previous code, (e-1) represents the total number of datasets. However, I keep getting { is a invalid character for the line: X= Application.WorksheetFunction.LinEst(Frac_y,Frac_x,{1,2}) Then I did some researches and modified the code to:

Dim Frac_x, Frac_y As Range
Dim X
        Set Frac_x = Range(Cells(17, 1), Cells(e - 1, 1))
        Set Frac_y = Range(Cells(17, 7), Cells(e - 1, 7))
        X = Application.Evaluate("=linest(" & Frac_y & "," & Frac_x & "^ {1,2}))")
        Cells(3, 8).Value = Application.WorksheetFunction.Index(X, 1, 1)
        Cells(4, 8).Value = Application.WorksheetFunction.Index(X, 1, 2)
        Cells(5, 8).Value = Application.WorksheetFunction.Index(X, 1, 3)

Then I keep getting Type Dismatch error for the line: X = Application.Evaluate("=linest(" & Frac_y & "," & Frac_x & "^ {1,2}))") I am sure the two ranges frac_y and frac_x their type matches. Anyone could help?


回答1:


You are right, that Excel VBA can't do things like arrVariable^{1,2}. That must be done with loops over the array items.

But the Evaluate approach should work. But your formula string is not correct. To detect and avoid such incorrectness, I will ever concatenate such formula strings within a String variable first. Then I can simply check the variable's value.

Example, Values are in A17:A26 and G17:G26:

Sub test()

 Dim Frac_x As Range, Frac_y As Range
 Dim X

 e = 27

 With ActiveSheet

  Set Frac_x = .Range(.Cells(17, 1), .Cells(e - 1, 1))
  Set Frac_y = .Range(.Cells(17, 7), .Cells(e - 1, 7))
  arrX = Frac_x
  ReDim arrX2(1 To UBound(arrX), 1 To 2) As Double
  For i = LBound(arrX) To UBound(arrX)
   arrX2(i, 1) = arrX(i, 1)
   arrX2(i, 2) = arrX(i, 1) * arrX(i, 1)
  Next

  X = Application.LinEst(Frac_y, arrX2)

  'sFormula = "=LINEST(" & Frac_y.Address & "," & Frac_x.Address & "^{1,2})"
  'X = Application.Evaluate(sFormula)

  .Range(.Cells(3, 8), .Cells(5, 8)).Value = Application.Transpose(X)

 End With

End Sub

Hints: Use Application.LinEst instead of Application.WorksheetFunction.LinEst. The latter will throw an error if the function cannot work while the first will return an error value instead. So the first will not interrupt the program as the latter will do.



来源:https://stackoverflow.com/questions/35542670/vba-use-linest-to-calculate-polynomial-coefficients-and-index-to-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!