Loop whose endpoint (?) change during the loopRefer to other cells besides the one in the Cells.FindMatching between files and a list of filenames at scaleMaintaining row height while insertingShow all full/partial search matches in the first worksheet from other worksheetsVBA faster calculatingA loop that assembles an Excel sheet by assembling matches from other sheetsSlow VBA macro using nested loops and autofilter to consolidate select data from 2 worksheets into 1Housing a summary of information in a data fileSpeed up array loop vba ExcelOOP Cross Sum Solver

10 year ban after applying for a UK student visa

Why is participating in the European Parliamentary elections used as a threat?

Hot air balloons as primitive bombers

Unfrosted light bulb

"Marked down as someone wanting to sell shares." What does that mean?

Is there any common country to visit for uk and schengen visa?

Exit shell with shortcut (not typing exit) that closes session properly

Jem'Hadar, something strange about their life expectancy

Can "few" be used as a subject? If so, what is the rule?

Have any astronauts/cosmonauts died in space?

What is the tangent at a sharp point on a curve?

Symbolism of 18 Journeyers

Should a narrator ever describe things based on a characters view instead of fact?

Is xar preinstalled on macOS?

Was World War I a war of liberals against authoritarians?

Determine voltage drop over 10G resistors with cheap multimeter

Should I be concerned about student access to a test bank?

What is it called when someone votes for an option that's not their first choice?

Hackerrank All Women's Codesprint 2019: Name the Product

Print last inputted byte

Animating wave motion in water

What (if any) is the reason to buy in small local stores?

How old is Nick Fury?

Homology of the fiber



Loop whose endpoint (?) change during the loop


Refer to other cells besides the one in the Cells.FindMatching between files and a list of filenames at scaleMaintaining row height while insertingShow all full/partial search matches in the first worksheet from other worksheetsVBA faster calculatingA loop that assembles an Excel sheet by assembling matches from other sheetsSlow VBA macro using nested loops and autofilter to consolidate select data from 2 worksheets into 1Housing a summary of information in a data fileSpeed up array loop vba ExcelOOP Cross Sum Solver













1












$begingroup$


I have a loop which insert rows on specific cases. The loop goes through ~2000 rows and insert about 500.



Due to the inserted rows it doesn't loop the full range I want. I have solved that problem by counting the number of row I will insert with a first loop and then looping up to LastRow + Counter.



I feel as if there must have been a better way to do it.



Sub AddRows()
Dim i As Integer: i = 8
Dim LastRow As Integer: LastRow = Cells(8, 2).End(xlDown).Row
Dim counter As Integer: counter = 0

For i = 8 To LastRow
If Cells(i, 1) <> "" Then
counter = counter + 1
End If
Next i

NewLastRowAfterAddingRows = LastRow + counter

For i = 8 To NewLastRowAfterAddingRows
If Cells(i, 1) <> "" Then
Range(i + 1 & ":" & i + 1).Insert CopyOrigin:=xlFormatFromRightOrBelow
i = i + 1
End If
Next i


End Sub









share|improve this question











$endgroup$
















    1












    $begingroup$


    I have a loop which insert rows on specific cases. The loop goes through ~2000 rows and insert about 500.



    Due to the inserted rows it doesn't loop the full range I want. I have solved that problem by counting the number of row I will insert with a first loop and then looping up to LastRow + Counter.



    I feel as if there must have been a better way to do it.



    Sub AddRows()
    Dim i As Integer: i = 8
    Dim LastRow As Integer: LastRow = Cells(8, 2).End(xlDown).Row
    Dim counter As Integer: counter = 0

    For i = 8 To LastRow
    If Cells(i, 1) <> "" Then
    counter = counter + 1
    End If
    Next i

    NewLastRowAfterAddingRows = LastRow + counter

    For i = 8 To NewLastRowAfterAddingRows
    If Cells(i, 1) <> "" Then
    Range(i + 1 & ":" & i + 1).Insert CopyOrigin:=xlFormatFromRightOrBelow
    i = i + 1
    End If
    Next i


    End Sub









    share|improve this question











    $endgroup$














      1












      1








      1





      $begingroup$


      I have a loop which insert rows on specific cases. The loop goes through ~2000 rows and insert about 500.



      Due to the inserted rows it doesn't loop the full range I want. I have solved that problem by counting the number of row I will insert with a first loop and then looping up to LastRow + Counter.



      I feel as if there must have been a better way to do it.



      Sub AddRows()
      Dim i As Integer: i = 8
      Dim LastRow As Integer: LastRow = Cells(8, 2).End(xlDown).Row
      Dim counter As Integer: counter = 0

      For i = 8 To LastRow
      If Cells(i, 1) <> "" Then
      counter = counter + 1
      End If
      Next i

      NewLastRowAfterAddingRows = LastRow + counter

      For i = 8 To NewLastRowAfterAddingRows
      If Cells(i, 1) <> "" Then
      Range(i + 1 & ":" & i + 1).Insert CopyOrigin:=xlFormatFromRightOrBelow
      i = i + 1
      End If
      Next i


      End Sub









      share|improve this question











      $endgroup$




      I have a loop which insert rows on specific cases. The loop goes through ~2000 rows and insert about 500.



      Due to the inserted rows it doesn't loop the full range I want. I have solved that problem by counting the number of row I will insert with a first loop and then looping up to LastRow + Counter.



      I feel as if there must have been a better way to do it.



      Sub AddRows()
      Dim i As Integer: i = 8
      Dim LastRow As Integer: LastRow = Cells(8, 2).End(xlDown).Row
      Dim counter As Integer: counter = 0

      For i = 8 To LastRow
      If Cells(i, 1) <> "" Then
      counter = counter + 1
      End If
      Next i

      NewLastRowAfterAddingRows = LastRow + counter

      For i = 8 To NewLastRowAfterAddingRows
      If Cells(i, 1) <> "" Then
      Range(i + 1 & ":" & i + 1).Insert CopyOrigin:=xlFormatFromRightOrBelow
      i = i + 1
      End If
      Next i


      End Sub






      vba excel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 mins ago









      Jamal

      30.4k11121227




      30.4k11121227










      asked 8 hours ago









      a1a1a1a1a1a1a1a1a1a1

      244




      244




















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

          It's generally not a good idea to alter the iterator variable in a for/next loop. While your code works, it's just not a good practice. Also, your initializing i to 8 is redundant since your loop starts at 8 as well. Here's how I would handle it:



          Sub AddRows()

          Dim i As Integer
          Dim LastRow As Integer
          Dim counter As Integer

          LastRow = Cells(8, 2).End(xlDown).Row
          counter = 8

          For i = counter To LastRow
          If Cells(counter, 1) <> "" Then
          counter = counter + 1
          Range(counter & ":" & counter).Insert CopyOrigin:=xlFormatFromRightOrBelow
          End If
          counter = counter + 1
          Next i

          End Sub





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thank you. I like yours better.
            $endgroup$
            – a1a1a1a1a1
            5 hours ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f215693%2floop-whose-endpoint-change-during-the-loop%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









          2












          $begingroup$

          It's generally not a good idea to alter the iterator variable in a for/next loop. While your code works, it's just not a good practice. Also, your initializing i to 8 is redundant since your loop starts at 8 as well. Here's how I would handle it:



          Sub AddRows()

          Dim i As Integer
          Dim LastRow As Integer
          Dim counter As Integer

          LastRow = Cells(8, 2).End(xlDown).Row
          counter = 8

          For i = counter To LastRow
          If Cells(counter, 1) <> "" Then
          counter = counter + 1
          Range(counter & ":" & counter).Insert CopyOrigin:=xlFormatFromRightOrBelow
          End If
          counter = counter + 1
          Next i

          End Sub





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thank you. I like yours better.
            $endgroup$
            – a1a1a1a1a1
            5 hours ago















          2












          $begingroup$

          It's generally not a good idea to alter the iterator variable in a for/next loop. While your code works, it's just not a good practice. Also, your initializing i to 8 is redundant since your loop starts at 8 as well. Here's how I would handle it:



          Sub AddRows()

          Dim i As Integer
          Dim LastRow As Integer
          Dim counter As Integer

          LastRow = Cells(8, 2).End(xlDown).Row
          counter = 8

          For i = counter To LastRow
          If Cells(counter, 1) <> "" Then
          counter = counter + 1
          Range(counter & ":" & counter).Insert CopyOrigin:=xlFormatFromRightOrBelow
          End If
          counter = counter + 1
          Next i

          End Sub





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thank you. I like yours better.
            $endgroup$
            – a1a1a1a1a1
            5 hours ago













          2












          2








          2





          $begingroup$

          It's generally not a good idea to alter the iterator variable in a for/next loop. While your code works, it's just not a good practice. Also, your initializing i to 8 is redundant since your loop starts at 8 as well. Here's how I would handle it:



          Sub AddRows()

          Dim i As Integer
          Dim LastRow As Integer
          Dim counter As Integer

          LastRow = Cells(8, 2).End(xlDown).Row
          counter = 8

          For i = counter To LastRow
          If Cells(counter, 1) <> "" Then
          counter = counter + 1
          Range(counter & ":" & counter).Insert CopyOrigin:=xlFormatFromRightOrBelow
          End If
          counter = counter + 1
          Next i

          End Sub





          share|improve this answer









          $endgroup$



          It's generally not a good idea to alter the iterator variable in a for/next loop. While your code works, it's just not a good practice. Also, your initializing i to 8 is redundant since your loop starts at 8 as well. Here's how I would handle it:



          Sub AddRows()

          Dim i As Integer
          Dim LastRow As Integer
          Dim counter As Integer

          LastRow = Cells(8, 2).End(xlDown).Row
          counter = 8

          For i = counter To LastRow
          If Cells(counter, 1) <> "" Then
          counter = counter + 1
          Range(counter & ":" & counter).Insert CopyOrigin:=xlFormatFromRightOrBelow
          End If
          counter = counter + 1
          Next i

          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          Bill HilemanBill Hileman

          1766




          1766











          • $begingroup$
            Thank you. I like yours better.
            $endgroup$
            – a1a1a1a1a1
            5 hours ago
















          • $begingroup$
            Thank you. I like yours better.
            $endgroup$
            – a1a1a1a1a1
            5 hours ago















          $begingroup$
          Thank you. I like yours better.
          $endgroup$
          – a1a1a1a1a1
          5 hours ago




          $begingroup$
          Thank you. I like yours better.
          $endgroup$
          – a1a1a1a1a1
          5 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f215693%2floop-whose-endpoint-change-during-the-loop%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

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar