How to Set Up Loop to do Goal Seek of Range with Changing Rows and Columns

mackie72

New Member
Joined
Aug 6, 2013
Messages
2
Hi,

I just joined this forum and I am only at beginner level with VB for Excel. I am looking for help to learn how to set up a loop to do something that I have already created a macro to do am looking for a way to make the coding more efficient and condensed. The existing macro looks like this:

Range("AM8").GoalSeek Goal:=0, ChangingCell:=Range("Y30")
Range("AM9").GoalSeek Goal:=0, ChangingCell:=Range("Y31")
Range("AM10").GoalSeek Goal:=0, ChangingCell:=Range("Y32")
Range("AM11").GoalSeek Goal:=0, ChangingCell:=Range("Y33")
Range("AM12").GoalSeek Goal:=0, ChangingCell:=Range("Y34")
Range("AM13").GoalSeek Goal:=0, ChangingCell:=Range("Y35")
Range("AM14").GoalSeek Goal:=0, ChangingCell:=Range("Y36")
Range("AM15").GoalSeek Goal:=0, ChangingCell:=Range("Y37")
Range("AM16").GoalSeek Goal:=0, ChangingCell:=Range("Y38")
Range("AM17").GoalSeek Goal:=0, ChangingCell:=Range("Y39")
Range("AM18").GoalSeek Goal:=0, ChangingCell:=Range("Y40")
Range("AM19").GoalSeek Goal:=0, ChangingCell:=Range("Y41")
Range("AM20").GoalSeek Goal:=0, ChangingCell:=Range("Y42")
Range("AM21").GoalSeek Goal:=0, ChangingCell:=Range("Y43")
Range("AM22").GoalSeek Goal:=0, ChangingCell:=Range("Y44")
Range("AM23").GoalSeek Goal:=0, ChangingCell:=Range("Y45")
Range("AM24").GoalSeek Goal:=0, ChangingCell:=Range("Y46")
Range("AM25").GoalSeek Goal:=0, ChangingCell:=Range("Y47")
Range("AN8").GoalSeek Goal:=0, ChangingCell:=Range("Z30")
Range("AN9").GoalSeek Goal:=0, ChangingCell:=Range("Z31")
Range("AN10").GoalSeek Goal:=0, ChangingCell:=Range("Z32")
Range("AN11").GoalSeek Goal:=0, ChangingCell:=Range("Z33")
Range("AN12").GoalSeek Goal:=0, ChangingCell:=Range("Z34")
Range("AN13").GoalSeek Goal:=0, ChangingCell:=Range("Z35")
Range("AN14").GoalSeek Goal:=0, ChangingCell:=Range("Z36")
Range("AN15").GoalSeek Goal:=0, ChangingCell:=Range("Z37")
Range("AN16").GoalSeek Goal:=0, ChangingCell:=Range("Z38")
Range("AN17").GoalSeek Goal:=0, ChangingCell:=Range("Z39")
Range("AN18").GoalSeek Goal:=0, ChangingCell:=Range("Z40")
Range("AN19").GoalSeek Goal:=0, ChangingCell:=Range("Z41")
Range("AN20").GoalSeek Goal:=0, ChangingCell:=Range("Z42")
Range("AN21").GoalSeek Goal:=0, ChangingCell:=Range("Z43")
Range("AN22").GoalSeek Goal:=0, ChangingCell:=Range("Z44")
Range("AN23").GoalSeek Goal:=0, ChangingCell:=Range("Z45")
Range("AN24").GoalSeek Goal:=0, ChangingCell:=Range("Z46")
Range("AN25").GoalSeek Goal:=0, ChangingCell:=Range("Z47")
Range("AO8").GoalSeek Goal:=0, ChangingCell:=Range("AA30")
Range("AO9").GoalSeek Goal:=0, ChangingCell:=Range("AA31")
Range("AO10").GoalSeek Goal:=0, ChangingCell:=Range("AA32")
Range("AO11").GoalSeek Goal:=0, ChangingCell:=Range("AA33")
Range("AO12").GoalSeek Goal:=0, ChangingCell:=Range("AA34")
Range("AO13").GoalSeek Goal:=0, ChangingCell:=Range("AA35")
Range("AO14").GoalSeek Goal:=0, ChangingCell:=Range("AA36")
Range("AO15").GoalSeek Goal:=0, ChangingCell:=Range("AA37")
Range("AO16").GoalSeek Goal:=0, ChangingCell:=Range("AA38")
Range("AO17").GoalSeek Goal:=0, ChangingCell:=Range("AA39")
Range("AO18").GoalSeek Goal:=0, ChangingCell:=Range("AA40")
Range("AO19").GoalSeek Goal:=0, ChangingCell:=Range("AA41")
Range("AO20").GoalSeek Goal:=0, ChangingCell:=Range("AA42")
Range("AO21").GoalSeek Goal:=0, ChangingCell:=Range("AA43")
Range("AO22").GoalSeek Goal:=0, ChangingCell:=Range("AA44")
Range("AO23").GoalSeek Goal:=0, ChangingCell:=Range("AA45")
Range("AO24").GoalSeek Goal:=0, ChangingCell:=Range("AA46")
Range("AO25").GoalSeek Goal:=0, ChangingCell:=Range("AA47")
Range("AP8").GoalSeek Goal:=0, ChangingCell:=Range("AB30")
Range("AP9").GoalSeek Goal:=0, ChangingCell:=Range("AB31")
Range("AP10").GoalSeek Goal:=0, ChangingCell:=Range("AB32")
Range("AP11").GoalSeek Goal:=0, ChangingCell:=Range("AB33")
Range("AP12").GoalSeek Goal:=0, ChangingCell:=Range("AB34")
Range("AP13").GoalSeek Goal:=0, ChangingCell:=Range("AB35")
Range("AP14").GoalSeek Goal:=0, ChangingCell:=Range("AB36")
Range("AP15").GoalSeek Goal:=0, ChangingCell:=Range("AB37")
Range("AP16").GoalSeek Goal:=0, ChangingCell:=Range("AB38")
Range("AP17").GoalSeek Goal:=0, ChangingCell:=Range("AB39")
Range("AP18").GoalSeek Goal:=0, ChangingCell:=Range("AB40")
Range("AP19").GoalSeek Goal:=0, ChangingCell:=Range("AB41")
Range("AP20").GoalSeek Goal:=0, ChangingCell:=Range("AB42")
Range("AP21").GoalSeek Goal:=0, ChangingCell:=Range("AB43")
Range("AP22").GoalSeek Goal:=0, ChangingCell:=Range("AB44")
Range("AP23").GoalSeek Goal:=0, ChangingCell:=Range("AB45")
Range("AP24").GoalSeek Goal:=0, ChangingCell:=Range("AB46")
Range("AP25").GoalSeek Goal:=0, ChangingCell:=Range("AB47")
Range("AQ8").GoalSeek Goal:=0, ChangingCell:=Range("AC30")
Range("AQ9").GoalSeek Goal:=0, ChangingCell:=Range("AC31")
Range("AQ10").GoalSeek Goal:=0, ChangingCell:=Range("AC32")
Range("AQ11").GoalSeek Goal:=0, ChangingCell:=Range("AC33")
Range("AQ12").GoalSeek Goal:=0, ChangingCell:=Range("AC34")
Range("AQ13").GoalSeek Goal:=0, ChangingCell:=Range("AC35")
Range("AQ14").GoalSeek Goal:=0, ChangingCell:=Range("AC36")
Range("AQ15").GoalSeek Goal:=0, ChangingCell:=Range("AC37")
Range("AQ16").GoalSeek Goal:=0, ChangingCell:=Range("AC38")
Range("AQ17").GoalSeek Goal:=0, ChangingCell:=Range("AC39")
Range("AQ18").GoalSeek Goal:=0, ChangingCell:=Range("AC40")
Range("AQ19").GoalSeek Goal:=0, ChangingCell:=Range("AC41")
Range("AQ20").GoalSeek Goal:=0, ChangingCell:=Range("AC42")
Range("AQ21").GoalSeek Goal:=0, ChangingCell:=Range("AC43")
Range("AQ22").GoalSeek Goal:=0, ChangingCell:=Range("AC44")
Range("AQ23").GoalSeek Goal:=0, ChangingCell:=Range("AC45")
Range("AQ24").GoalSeek Goal:=0, ChangingCell:=Range("AC46")
Range("AQ25").GoalSeek Goal:=0, ChangingCell:=Range("AC47")

So the cells to be "goal-seeked" start with AM8 continuing down column AM until row 25, by changing Y30, staying in column Y until row 47. Then the macro moves to the next column and goal-seeks rows 8-25 in AN, then AO, then AP then AQ.

Any help to so I can learn how to reduce this to a loop instead of manually doing the whole thing would be greatly appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this Loop. It is untested as I did not create a Solver demo spreadsheet
Code:
Sub ams()
Dim i As Integer
i = i + 1
For i = 8 To 25
Range("AM" & i).GoalSeek Goal:=0, ChangingCell:=Range("Y" & i + 12)
Range("AN" & i).GoalSeek Goal:=0, ChangingCell:=Range("Z" & i + 12)
Range("AO" & i).GoalSeek Goal:=0, ChangingCell:=Range("AA" & i + 12)
Range("AP" & i).GoalSeek Goal:=0, ChangingCell:=Range("AB" & i + 12)
Range("AQ" & i).GoalSeek Goal:=0, ChangingCell:=Range("AC" & i + 12)
Next i


End Sub
 
Upvote 0
Thank you so much Alan! One small note is that the adder should be 22 instead of 12, but I got the idea. Do you have any tips on what course I should take or book I should buy etc, that you think is the best way to learn VBA coding?
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,099
Members
452,301
Latest member
QualityAssurance

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