PMarinshaw
Mechanical
- Jun 19, 2002
- 47
A little backgound. Many years ago I did some basic programming with VB, but haven't touched it for ~ 10 years. I am running a complicated macro in Excel which loops through over 10K separate calculations and have come across a snag. I'm using the GoalSeek imbedded function within Excel to calculate a "what if" scenario - what value of a given cell causes another cell to equate to 0. Unfortunately, in several instances the GoalSeek function does not converge and the result is garbage. So I thought about supplementing my VB macro with a Do Loop to increment my "changing" cell value until the value of my desired resultant cell = 0. In a test code to establish how I want it to work I can't get the variable CALC to recognize the value in cell C3. Can someone help me with a VB 101 resolution? CALC evaluates to -1 rather than C8-C3, takes on a fixed value of 501 and the loop becomes infinite. Here is my code:
Sub Macro1 ()
Dim INC, PRESS, TEST, STRESS As Integer
Dim CALC As Variant
PRESS = 100
INC = 1
STRESS = 500
Range("C8").Select
ActiveCell.FormulaR1C1 = STRESS
CALC = Range("C3").Select
TEST = STRESS - CALC
Range("B1").Select
ActiveCell.FormulaR1C1 = "99"
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = PRESS
Range("C6").Select
ActiveCell.FormulaR1C1 = TEST
TEST = STRESS - CALC
Range("C6").Select
ActiveCell.FormulaR1C1 = TEST
Do While TEST > 0
PRESS = PRESS + INC
Range("B1").Select
ActiveCell.FormulaR1C1 = PRESS
Loop
End Sub
Thanks in Advance.
Sub Macro1 ()
Dim INC, PRESS, TEST, STRESS As Integer
Dim CALC As Variant
PRESS = 100
INC = 1
STRESS = 500
Range("C8").Select
ActiveCell.FormulaR1C1 = STRESS
CALC = Range("C3").Select
TEST = STRESS - CALC
Range("B1").Select
ActiveCell.FormulaR1C1 = "99"
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = PRESS
Range("C6").Select
ActiveCell.FormulaR1C1 = TEST
TEST = STRESS - CALC
Range("C6").Select
ActiveCell.FormulaR1C1 = TEST
Do While TEST > 0
PRESS = PRESS + INC
Range("B1").Select
ActiveCell.FormulaR1C1 = PRESS
Loop
End Sub
Thanks in Advance.