Hi,
I have a code which converges in a loop. It is referenced to 4 cells in a row. I want to change it so its dyamic and each time the macro runs to calculate all the active rows in my spread sheet. This is my code have have so far (looks long and complicated I know)
Sub Worksheet_Calculate()
Dim dblD As Double
Dim dblX As Double
Dim dblHL As Double
Dim dblHLOverD As Double
Dim dblTheta As Double
Dim dblCosTheta As Double
Dim dblF2 As Double
Dim dblHHL As Double
Dim myCell As Range
Static bWorking As Boolean
Const PI = 3.141592654
If bWorking = False Then
bWorking = True
'Get the value for fraction occupancy
Set myCell = Range("X11")
dblX = CDbl(myCell.Value)
'Get the value for diameter
Set myCell = Range("AB11")
dblD = myCell.Value
'Get the value for heavy liquid depth
Set myCell = Range("AL11")
dblHHL = myCell.Value
dblHL = dblD / 2
Do
dblCosTheta = (1 - 2 * dblHL / dblD)
dblTheta = ACOS(1 - 2 * dblHL / dblD)
dblF2 = (dblTheta / PI) - (Sin(dblTheta) * Cos(dblTheta) / PI)
dblHLOverD = dblHL / dblD
'Check for convergence
If Abs(dblX - dblF2) < 0.00001 Then
Exit Do
End If
'Adjust HL
dblHL = dblHL * dblX / dblF2
DoEvents
Loop
Set myCell = Range("AM11")
Debug.Print dblHL - dblHHL
'Write result
myCell.Value = dblHL - dblHHL
bWorking = False
End If
End Sub
The 4 cells that are reference I want to change-I have managed this with goal seek where it was referenced to 3 cells in row 11 but found a way to make it work for the whole sheet-eg
Sub AutoGoalSeek()
Dim x As Long
Dim LastRow As Long
Const StartRow As Long = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 2).End(xlUp).Row
For x = StartRow To LastRow
If .Range("AK" & x).HasFormula Then
.Range("AK" & x).GoalSeek Goal:=.Range("AI" & x), _
ChangingCell:=.Range("AJ" & x)
End If
Next x
End With
End Sub
I have tryed changing the range in the top code to be Set myRange = Range("X" & x)
but this gives me the error "Method 'Range' of object'_Gobal' failed.
Please can anyone help me change the top code to work dyamically like the 2nd code.
Thank you in Advanced.
Ann-Marie
I have a code which converges in a loop. It is referenced to 4 cells in a row. I want to change it so its dyamic and each time the macro runs to calculate all the active rows in my spread sheet. This is my code have have so far (looks long and complicated I know)
Sub Worksheet_Calculate()
Dim dblD As Double
Dim dblX As Double
Dim dblHL As Double
Dim dblHLOverD As Double
Dim dblTheta As Double
Dim dblCosTheta As Double
Dim dblF2 As Double
Dim dblHHL As Double
Dim myCell As Range
Static bWorking As Boolean
Const PI = 3.141592654
If bWorking = False Then
bWorking = True
'Get the value for fraction occupancy
Set myCell = Range("X11")
dblX = CDbl(myCell.Value)
'Get the value for diameter
Set myCell = Range("AB11")
dblD = myCell.Value
'Get the value for heavy liquid depth
Set myCell = Range("AL11")
dblHHL = myCell.Value
dblHL = dblD / 2
Do
dblCosTheta = (1 - 2 * dblHL / dblD)
dblTheta = ACOS(1 - 2 * dblHL / dblD)
dblF2 = (dblTheta / PI) - (Sin(dblTheta) * Cos(dblTheta) / PI)
dblHLOverD = dblHL / dblD
'Check for convergence
If Abs(dblX - dblF2) < 0.00001 Then
Exit Do
End If
'Adjust HL
dblHL = dblHL * dblX / dblF2
DoEvents
Loop
Set myCell = Range("AM11")
Debug.Print dblHL - dblHHL
'Write result
myCell.Value = dblHL - dblHHL
bWorking = False
End If
End Sub
The 4 cells that are reference I want to change-I have managed this with goal seek where it was referenced to 3 cells in row 11 but found a way to make it work for the whole sheet-eg
Sub AutoGoalSeek()
Dim x As Long
Dim LastRow As Long
Const StartRow As Long = 1
With ActiveSheet
LastRow = .Cells(Rows.Count, 2).End(xlUp).Row
For x = StartRow To LastRow
If .Range("AK" & x).HasFormula Then
.Range("AK" & x).GoalSeek Goal:=.Range("AI" & x), _
ChangingCell:=.Range("AJ" & x)
End If
Next x
End With
End Sub
I have tryed changing the range in the top code to be Set myRange = Range("X" & x)
but this gives me the error "Method 'Range' of object'_Gobal' failed.
Please can anyone help me change the top code to work dyamically like the 2nd code.
Thank you in Advanced.
Ann-Marie