title: VB -Copy and Paste Nested Loop in Excel
body: <p>So I have a problem that this is generating random results with the Qty.</p>
  
  <p>I am trying to make each qty (in their qty's) a new line on a new spreadsheet.</p>
  
  <p>It creates the new sheet, and references the old sheet...
  the code copies and pastes the lines...
  It just doesn't loop the do while in the correct amount of times. I have tried different operands (>= 0) and altering the variable values to make this work. </p>
  
  <p>It does not seem to be patternized as to why it is happening. Sometimes it does it in the correct amount of loop cycles, others it does not. This occurs on multiple values. Any help is greatly appreciated.</p>
  
  <pre><code>Sub copyPasta()
  '
  ' copyPasta Macro
  ' This will take the qty, if greater than one  in Column C and copy the row 
  'to a new sheet the amount of time the qty.
  '
  '
  'Set Variable Types
  Dim lineItemQty As Integer
  Dim newLineItemQty As Integer
  Dim LastRow As Integer
  Dim strSheetName As String
  Dim newSheetName As String
  Dim i As Integer
  
  Application.DisplayAlerts = False
  
  'name a variable after the existing active sheet
  strSheetName = ActiveSheet.Name
  'add a sheet in addition to the current
  Sheets.Add After:=ActiveSheet
  'set a variable used in loops to the sheet being copied to
  newSheetName = ActiveSheet.Name
  'Return to first sheet
  Sheets(strSheetName).Activate
  ' Set For Loop to max row
  LastRow = Sheets(strSheetName).Range("C:C").Find("*", searchdirection:=xlPrevious).Row
  
  'for loop to run through all rows
  For i = 3 To LastRow Step 1
  
      'initializing variable to Qty value in table
      lineItemQty = Range("C" &amp; i).Value
  
      'initializing variable within in line of for looping
      newLineItemQty = lineItemQty
  
      'do while loop to keep copying/pasting while there are still qty's
          Do While newLineItemQty &gt; 0
  
          'do while looped copy and paste
              'copy the active row
                  Sheets(strSheetName).Activate
                  Rows(i).Select
                  Selection.Copy
              'paste active row into new sheet
                  Sheets(newSheetName).Select
                  Rows("3:3").Select
                  Selection.Insert Shift:=xlDown
  
  
              newLineItemQty = newLineItemQty - 1
  
          Loop
  Next i
  
  Application.DisplayAlerts = True
  
  End Sub
  </code></pre>