elliotawin
New Member
- Joined
- Jan 22, 2014
- Messages
- 5
I have been using the following Macro to set prices for a given Profit on Return.
(sounds somewhat unnecessary as I could use a formula the other way around - however it is necessary and this is the favored way of doing things)
However with a list of products there is inevitably a subheading and therefore a gap in the Target Address (Taddr) and the Adjusted Rang (Aaddr) I am sure there is a simple solution to skip a blank cell in the For loop, but i can;t seem to find it!!! Any suggestions..
Sub GSeekA()
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
' Create the following names in the back-solver worksheet:
' Taddr - Cell with the address of the target range
' Aaddr - Cell with the address of the range to be adjusted
' gval - the "Goal" value
' To reference ranges on different sheets also add:
' TSheet - Cell with the sheet name of the target range
' ASheet - Cell with the sheet name of the range to be adjusted
Aaddr = range("aaddr").Value
Taddr = range("taddr").Value
On Error GoTo NoSheetNames
ASheet = range("asheet").Value
TSheet = range("tsheet").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("gval").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
ExitSub:
End Sub
(sounds somewhat unnecessary as I could use a formula the other way around - however it is necessary and this is the favored way of doing things)
However with a list of products there is inevitably a subheading and therefore a gap in the Target Address (Taddr) and the Adjusted Rang (Aaddr) I am sure there is a simple solution to skip a blank cell in the For loop, but i can;t seem to find it!!! Any suggestions..
Sub GSeekA()
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
' Create the following names in the back-solver worksheet:
' Taddr - Cell with the address of the target range
' Aaddr - Cell with the address of the range to be adjusted
' gval - the "Goal" value
' To reference ranges on different sheets also add:
' TSheet - Cell with the sheet name of the target range
' ASheet - Cell with the sheet name of the range to be adjusted
Aaddr = range("aaddr").Value
Taddr = range("taddr").Value
On Error GoTo NoSheetNames
ASheet = range("asheet").Value
TSheet = range("tsheet").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("gval").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
ExitSub:
End Sub