Question

First of all, here is the Excel chart in question, before running the code:

enter image description here

and after running the code:

enter image description here

Here is the code in question:

Option Explicit

Dim Counter As Long
Dim DeSimpleFinal As Double

Sub SimpleDeCalculationNEW()

Counter = 13
Range("C1") = "CFL Calculated"
Range("D1") = "Residual Squared"
Range("E1") = "De value"
Range("F1") = 0.2

'Inserting Equations
Range("C2") = "=((2 * $H$2)/ $H$1) * SQR(($F$1 * A2) / PI())"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & Counter + 1), Type:=xlFillDefault

Range("D2") = "=((ABS(B2-C2))^2)"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D" & Counter + 1), Type:=xlFillDefault

'Summing up the residuals squared
Range("D" & Counter + 2) = "=Sum(D2, D" & Counter + 1 & ")"

Columns("A:Z").EntireColumn.EntireColumn.AutoFit

'Goal Seek
Range("D" & Counter + 2).GoalSeek Goal:=0, ChangingCell:=Range("F1")

DeSimpleFinal = Range("F1")
'Range("XFD1048523") = DeSimpleFinal

MsgBox ("The Final Value for DeSimple is: " & DeSimpleFinal)

End Sub

I keep getting this error, Runtime Error: 1004 Reference is Not Valid, and all of those name errors on the worksheet. Does anybody know how I can fix this? I'm trying to get the sum of the residuals squared as close to zero as possible, in order to get the best possible fit for my data. I do this by using Goal Seek, which is varying the value of De, given in F1, in order to minimize the sum of the residuals squared, given in D15.

I've gotten this method to work before without issue, and I'm not really sure why it's failing here. Any advice would be amazing!

Was it helpful?

Solution

I'm not sure if this fix all the problems but change your function line into the following:

Range("C2").Formula = "=((2 * $H$2)/ $H$1) * SQRT(($F$1 * A2) / PI())"

(SQRT instead of SQR)

Additional tip, sometimes it could be better to name correctly object properties, in your situation I would suggest to use .Formula when inserting function into range, like:

Range("C2").Formula = "=((2 * $H$2)/ $H$1) * SQRT(($F$1 * A2) / PI())"
Range("D2").Formula = "=((ABS(B2-C2))^2)"
Range("D" & Counter + 2).Formula = "=Sum(D2, D" & Counter + 1 & ")"

I know it's working usually what you have but my suggestion is a good habit which I stick to :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top