How to Use VBA to Loop goal seek down columns











up vote
0
down vote

favorite












I am new to this site and brand new to VBA. I have a question related to goal seek.



I need to use Goal seek in VBA several times. Here is a sample of my Excel Sheet:



Excel sheet



I need to set the cells in F(which is a formula linked to another sheet) to 0, by changing the cells in I (hardcoded). I have the basic code to do this, but I want to figure out how to tell Excel "hey, loop this action for every cell in the column". I don't want to have to manually put this command in VBA hundreds of times. Here is what I actually have in VBA.



Sub Goal_Seek()
'
' Goal_Seek Macro

Range("F7").Select
Range("F7").GoalSeek Goal:=0, ChangingCell:=Range("I7")
Range("F8").Select
Range("F8").GoalSeek Goal:=0, ChangingCell:=Range("I8")
Range("F9").Select
Range("F9").GoalSeek Goal:=0, ChangingCell:=Range("I9")
Range("F10").Select
Range("F10").GoalSeek Goal:=0, ChangingCell:=Range("I10")
Range("F11").Select
Range("F11").GoalSeek Goal:=0, ChangingCell:=Range("I11")
End Sub


Any help is appreciated.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I am new to this site and brand new to VBA. I have a question related to goal seek.



    I need to use Goal seek in VBA several times. Here is a sample of my Excel Sheet:



    Excel sheet



    I need to set the cells in F(which is a formula linked to another sheet) to 0, by changing the cells in I (hardcoded). I have the basic code to do this, but I want to figure out how to tell Excel "hey, loop this action for every cell in the column". I don't want to have to manually put this command in VBA hundreds of times. Here is what I actually have in VBA.



    Sub Goal_Seek()
    '
    ' Goal_Seek Macro

    Range("F7").Select
    Range("F7").GoalSeek Goal:=0, ChangingCell:=Range("I7")
    Range("F8").Select
    Range("F8").GoalSeek Goal:=0, ChangingCell:=Range("I8")
    Range("F9").Select
    Range("F9").GoalSeek Goal:=0, ChangingCell:=Range("I9")
    Range("F10").Select
    Range("F10").GoalSeek Goal:=0, ChangingCell:=Range("I10")
    Range("F11").Select
    Range("F11").GoalSeek Goal:=0, ChangingCell:=Range("I11")
    End Sub


    Any help is appreciated.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am new to this site and brand new to VBA. I have a question related to goal seek.



      I need to use Goal seek in VBA several times. Here is a sample of my Excel Sheet:



      Excel sheet



      I need to set the cells in F(which is a formula linked to another sheet) to 0, by changing the cells in I (hardcoded). I have the basic code to do this, but I want to figure out how to tell Excel "hey, loop this action for every cell in the column". I don't want to have to manually put this command in VBA hundreds of times. Here is what I actually have in VBA.



      Sub Goal_Seek()
      '
      ' Goal_Seek Macro

      Range("F7").Select
      Range("F7").GoalSeek Goal:=0, ChangingCell:=Range("I7")
      Range("F8").Select
      Range("F8").GoalSeek Goal:=0, ChangingCell:=Range("I8")
      Range("F9").Select
      Range("F9").GoalSeek Goal:=0, ChangingCell:=Range("I9")
      Range("F10").Select
      Range("F10").GoalSeek Goal:=0, ChangingCell:=Range("I10")
      Range("F11").Select
      Range("F11").GoalSeek Goal:=0, ChangingCell:=Range("I11")
      End Sub


      Any help is appreciated.










      share|improve this question















      I am new to this site and brand new to VBA. I have a question related to goal seek.



      I need to use Goal seek in VBA several times. Here is a sample of my Excel Sheet:



      Excel sheet



      I need to set the cells in F(which is a formula linked to another sheet) to 0, by changing the cells in I (hardcoded). I have the basic code to do this, but I want to figure out how to tell Excel "hey, loop this action for every cell in the column". I don't want to have to manually put this command in VBA hundreds of times. Here is what I actually have in VBA.



      Sub Goal_Seek()
      '
      ' Goal_Seek Macro

      Range("F7").Select
      Range("F7").GoalSeek Goal:=0, ChangingCell:=Range("I7")
      Range("F8").Select
      Range("F8").GoalSeek Goal:=0, ChangingCell:=Range("I8")
      Range("F9").Select
      Range("F9").GoalSeek Goal:=0, ChangingCell:=Range("I9")
      Range("F10").Select
      Range("F10").GoalSeek Goal:=0, ChangingCell:=Range("I10")
      Range("F11").Select
      Range("F11").GoalSeek Goal:=0, ChangingCell:=Range("I11")
      End Sub


      Any help is appreciated.







      excel vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 18:21









      BigBen

      4,7752417




      4,7752417










      asked Nov 21 at 18:14









      austinmb

      11




      11
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Maybe something like:



          Sub Goal_Seek()
          Dim lastRow As Long, i As Long

          With ActiveSheet
          lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

          For i = 7 To lastRow
          .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)
          Next i
          End With
          End Sub





          share|improve this answer





















          • Are you saying to put this before the set of commands that I have?
            – austinmb
            Nov 21 at 18:34










          • No - this should be it. The For loop does what you want.
            – BigBen
            Nov 21 at 18:36










          • Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
            – austinmb
            Nov 21 at 19:05












          • Maybe here
            – BigBen
            Nov 21 at 19:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418253%2fhow-to-use-vba-to-loop-goal-seek-down-columns%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Maybe something like:



          Sub Goal_Seek()
          Dim lastRow As Long, i As Long

          With ActiveSheet
          lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

          For i = 7 To lastRow
          .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)
          Next i
          End With
          End Sub





          share|improve this answer





















          • Are you saying to put this before the set of commands that I have?
            – austinmb
            Nov 21 at 18:34










          • No - this should be it. The For loop does what you want.
            – BigBen
            Nov 21 at 18:36










          • Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
            – austinmb
            Nov 21 at 19:05












          • Maybe here
            – BigBen
            Nov 21 at 19:07















          up vote
          0
          down vote













          Maybe something like:



          Sub Goal_Seek()
          Dim lastRow As Long, i As Long

          With ActiveSheet
          lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

          For i = 7 To lastRow
          .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)
          Next i
          End With
          End Sub





          share|improve this answer





















          • Are you saying to put this before the set of commands that I have?
            – austinmb
            Nov 21 at 18:34










          • No - this should be it. The For loop does what you want.
            – BigBen
            Nov 21 at 18:36










          • Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
            – austinmb
            Nov 21 at 19:05












          • Maybe here
            – BigBen
            Nov 21 at 19:07













          up vote
          0
          down vote










          up vote
          0
          down vote









          Maybe something like:



          Sub Goal_Seek()
          Dim lastRow As Long, i As Long

          With ActiveSheet
          lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

          For i = 7 To lastRow
          .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)
          Next i
          End With
          End Sub





          share|improve this answer












          Maybe something like:



          Sub Goal_Seek()
          Dim lastRow As Long, i As Long

          With ActiveSheet
          lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

          For i = 7 To lastRow
          .Range("F" & i).GoalSeek Goal:=0, ChangingCell:=.Range("I" & i)
          Next i
          End With
          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 18:31









          BigBen

          4,7752417




          4,7752417












          • Are you saying to put this before the set of commands that I have?
            – austinmb
            Nov 21 at 18:34










          • No - this should be it. The For loop does what you want.
            – BigBen
            Nov 21 at 18:36










          • Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
            – austinmb
            Nov 21 at 19:05












          • Maybe here
            – BigBen
            Nov 21 at 19:07


















          • Are you saying to put this before the set of commands that I have?
            – austinmb
            Nov 21 at 18:34










          • No - this should be it. The For loop does what you want.
            – BigBen
            Nov 21 at 18:36










          • Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
            – austinmb
            Nov 21 at 19:05












          • Maybe here
            – BigBen
            Nov 21 at 19:07
















          Are you saying to put this before the set of commands that I have?
          – austinmb
          Nov 21 at 18:34




          Are you saying to put this before the set of commands that I have?
          – austinmb
          Nov 21 at 18:34












          No - this should be it. The For loop does what you want.
          – BigBen
          Nov 21 at 18:36




          No - this should be it. The For loop does what you want.
          – BigBen
          Nov 21 at 18:36












          Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
          – austinmb
          Nov 21 at 19:05






          Can you point me to somewhere where I can learn about the syntax?— for ex: what’s “as long” and what’s .end(xlup).row? Finally “For... next” thanks!
          – austinmb
          Nov 21 at 19:05














          Maybe here
          – BigBen
          Nov 21 at 19:07




          Maybe here
          – BigBen
          Nov 21 at 19:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418253%2fhow-to-use-vba-to-loop-goal-seek-down-columns%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Lallio

          Unable to find Lightning Node

          Futebolista