title: Excel Macro- Run-time error '9': Subscript out of range (within For Loop to copy)
body: <p>This is a simple macro code to copy range A2 until last row from one workbook and paste the same in another workbook. I am a newbie and googled many pages but failed to help self. The code is pasted below-</p>
  
  <p>`</p>
  
  <pre><code>Sub TC_Creation_Sample()
  Dim aPath As String, aFile As String, bFile As String
  Dim FinalRow As Integer, x As Integer
  
  With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
  End With
  
  aPath = "C:\temp\"
  aFile = aPath &amp; "Config"
  bFile = aPath &amp; "TC_Template"
  Workbooks.Open (aFile)
  Sheets("Config").Activate
  'FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
  FinalRow = Worksheets("Config").Range("A2").End(xlDown)
  
  For x = 2 To FinalRow
              Worksheets("Config").Range("A" &amp; x).Select
              Selection.Copy
              Workbooks.Open (bFile)
              Worksheets("TestCases").Range("A" &amp; x).Select
              ActiveSheet.Paste
  Next x       
  End Sub
  </code></pre>
  
  <p>`</p>
  
  <p>Result- Upon debugging, it's found that the code is successfully copy pasting A2 value. However, it's throwing run-time error 9: Subscript out of range. And the error occurs when Worksheets("Config").Range("A" &amp; x).Select is running for the second time after Next x. I am not sure what's really causing this error. Kindly, advise.</p>