Goal Seek Macro - ignore blank cells in ranges

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
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try this:

Rich (BB code):
For j = 1 To NumCols
For i = 1 To NumRows
If not cells(i,j) =empty then
TRange.Cells(i, j).GoalSeek Goal:=GVal, ChangingCell:=ARange.Cells(i, j)
End if
Next i
Next j
 
Upvote 0
That is the solution I started with, it works for the first set of populated cells, then stops. i.e. AX434:AX438 even though the Adjusted Range is set to AX434:AX498 as there is a blank cell at AX439. There is no error function, it just stops.
 
Upvote 0
This should not stop the code. If it stopped check your NumCols and NumRows values.
 
Upvote 0
the problem is the GoalSeek there.

The "On Error GoTo ExitSub" caused that you don't have error message.

To be honest I don't know yet how to correct the GoalSeek to get it work correctly:(
 
Upvote 0
SORTED!
For j = 1 To NumCols
For i = 1 To NumRows
If Not TRange.Cells(i, j) = Empty Then
TRange.Cells(i, j).GoalSeek Goal:=GVal, ChangingCell:=ARange.Cells(i, j)
End If
Next i
Next j
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top