Multiple Goal Seek

mynam

New Member
Joined
Dec 14, 2011
Messages
10
Hi,

I have the following code for multiple goal seek.

Option Explicit Sub Multi_Goal_Seek() Dim TargetVal As Range, DesiredVal As Range, ChangeVal As Range, CVcheck As Range Dim CheckLen As Long, i As Long restart: With Application Set TargetVal = .InputBox(Title:="Select a range in a single row or column", _ prompt:="Select your range which contains the ""Set Cell"" range", Default:=Range("C11:E11").Address, Type:=8) 'no default option 'prompt:="Select your range which contains the ""Set Cell"" range",, Type:=8) Set DesiredVal = .InputBox(Title:="Select a range in a single row or column", _ prompt:="Select the range which the ""Set Cells"" will be changed to", Default:=Range("C12:E12").Address, Type:=8) 'no default option 'prompt:="Select the range which the ""Set Cells"" will be changed to",, Type:=8) Set ChangeVal = .InputBox(Title:="Select a range in a single row or column", _ prompt:="Select the range of cells that will be changed", Default:=Range("G8:G10").Address, Type:=8) 'no default option 'prompt:="Select the range of cells that will be changed",, Type:=8) End With 'Ensure that the changing cell range contains only values, no formulas allowed Set CVcheck = Intersect(ChangeVal, Union(Sheets(ChangeVal.Parent.Name).Cells.SpecialCells(xlBlanks), Sheets(ChangeVal.Parent.Name).Cells.SpecialCells(xlConstants))) If CVcheck Is Nothing Then MsgBox "Changing value range contains no blank cells or values" & vbNewLine & _ "Goal seek only works if the cells to be changed are values, please ensure that this is the case", vbCritical Application.Goto reference:=DesiredVal Exit Sub Else If CVcheck.Cells.Count <> DesiredVal.Cells.Count Then MsgBox "Changing value range contains formulas" & vbNewLine & _ "Goal seek only works if the cells to be changed are values, please ensure that this is the case", vbCritical Application.Goto reference:=DesiredVal Exit Sub End If End If 'Ensure that the amount of cells is consistent If TargetVal.Cells.Count <> DesiredVal.Cells.Count Or TargetVal.Cells.Count <> ChangeVal.Cells.Count Then CheckLen = MsgBox("Ranges were different lengths, please press yes to re-enter", vbYesNo + vbCritical) If CheckLen = vbYes Then 'If ranges are different sizes and user wants to redo then restart code Goto restart Else Exit Sub End If End If ' Loop through the goalseek method For i = 1 To TargetVal.Columns.Count TargetVal.Cells(i).GoalSeek Goal:=DesiredVal.Cells(i).Value, ChangingCell:=ChangeVal.Cells(i) Next i End Sub


In this code you have to select all the ranges & targeted values
manually. But I want to give cell ranges & the targeted value in separate cells so that the vba code will automatically identify those cell to run the macro.
For example, cell A1 will be target cell range :k1:k100
Cell A2 will be the cells to change which is m1:m100
Cell A3 will be the desired values of the target cells which is 1. Do i need a range here ie n1:n100 which all contain 1?

Really appreciate the help
Because I am running this macro over multiple sheets, the number of rows may change hence by defining the ranges in A1, A2, A3 on each sheet will make life easier

Thanks


</pre>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Welcome to the board.

That code is a little difficult to read. Carriage returns would help.
 
Upvote 0
Can you please re-post your code by click # button and then inserting your code?

Biz
 
Upvote 0
hi,
I have already solved this problem. Anyway thanks a lot for your help. I found this website is really helpful.

Thanks.
 
Upvote 0
Maybe next time we can help a little more :biggrin:

Good luck.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,638
Members
449,461
Latest member
kokoanutt

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