Hey all,
I have a macro that does an automatic goal seek in a specified range. For some occasions, the results are obvious nonsense, so I wanted to add limits to the function. Is there an easy way to do this?
See Code below.
Thanks
Sub GoalSeek1()
Dim ARange As Range, TRange As Range, Aaddr As String, Taddr As String, NumEq As Long, i As Long, j As Long
Dim TSheet As String, ASheet As String, NumRows As Long, NumCols As Long
Dim GVal As Double, Acell As Range, TCell As Range, Orient As String
Aaddr = Range("E42").Value
Taddr = Range("E41").Value
On Error GoTo NoSheetNames
ASheet = Range("C42").Value
TSheet = Range("C41").Value
NoSheetNames:
On Error GoTo ExitSub
If ASheet = Empty Or TSheet = Empty Then
Set ARange = Range(Aaddr)
Set TRange = Range(Taddr)
Else
Set ARange = Worksheets(ASheet).Range(Aaddr)
Set TRange = Worksheets(TSheet).Range(Taddr)
End If
NumRows = ARange.Rows.Count
NumCols = ARange.Columns.Count
GVal = Range("G41").Value
For j = 1 To NumCols
For i = 1 To NumRows
TRange.Cells(i, j).GoalSeek Goal:=GVal, ChangingCell:=ARange.Cells(i, j)
Next i
Next j
End If
ExitSub:
End Sub
Bookmarks