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
 
I changed the code before to have it as above and it still doesn't work on sample you sent. If it works for you glad to hear it :)

But to test it test value in row 7 from 74.65 to something like 80 and run code. on the sample you sent me code doesn't change it:(
 
Last edited:
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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