title: Trying to iterate through some workbooks from a list of workbooks, getting out of range errors
body: <p>I have a problem. I'm guessing its easier to first write the code, and then explain it so here goes:</p>
  
  <pre><code>Sub Test()
     Dim myHeadings() As String
     Dim i As Long
     Dim path As String
     Dim pathtwo As String
     Dim currentWb As Workbook
     Dim openWb As Workbook
     Dim openWs As Worksheet
     Set currentWb = ActiveWorkbook
     path = "C:\pathto\"
     pfile = Split("File1,File2,File3", ",")
     myHeadings = Split("Januari,Februari,Mars,April,Maj,Juni,Juli,Augusti,September,Oktober,November,December", ",")
  
   For j = 0 To UBound(pfile)
      pathtwo = path &amp; pfile(j) &amp; ".xlsx"
      i = 0
      If IsFile(pathtwo) = True Then
          For i = 0 To UBound(myHeadings)
              Set openWb = Workbooks.Open(pathtwo)
              Set openWs = openWb.Sheets(myHeadings(i))
              If openWs.Range("C34") = 0 Then
                  currentWb.Sheets("Indata").Cells(70, i  + 27 + 12*j.Value = ""
              Else
                  currentWb.Sheets("Indata").Cells(70, i + 27 + 12*j).Value = openWs.Range("C34")
              End If
         Next i
       End if
       Workbooks(openWb.Name).Close
  Next j
  End sub
  </code></pre>
  
  <p>What I want to pick a file from the pfile list, iterate through all its sheets defined in myHeadings and deduct the value at C34 (in reality there are plenty more values deducted, but to keep it short). After this I want to Close the file, go to the next file and do the same thing until all the Three files (again, in reality there are more, which some of them does not exist yet). </p>
  
  <p>The function "IsFile" is </p>
  
  <pre><code>Function IsFile(fName As String) As Boolean
  'Returns TRUE if the provided name points to an existing file.
  'Returns FALSE if not existing, or if it's a folder
     On Error Resume Next
     IsFile = ((GetAttr(fName) And vbDirectory) &lt;&gt; vbDirectory)
  End Function
  </code></pre>
  
  <p>written by iDevlop at stackoverflow, in this thread: <a href="https://stackoverflow.com/questions/16351249/vba-check-if-file-exists">VBA check if file exists</a></p>
  
  <p>The reason why I have </p>
  
  <pre><code> currentWb.Sheets("Indata").Cells(70, i + 27 + 12*j).Value = openWs.Range("C34")
  </code></pre>
  
  <p>is because I want to start to write my data into currentWb at AA70 (Row 70, column 27). j*12 is because it is "periodic" depending on which file it is (the file file1 corresponds to 2015, file2 to 2016 etc), and hence in my summary I have it month and yearwise. </p>
  
  <p>The problem arises though when I run this macro, at the first file at the sheet Mars I get out of range, but Before I added the iteration of files, there was not any subscript out of range at the first file. Is there anyone who can see how this can be? </p>
  
  <p>Please note that indentation and so on may be somewhat off as I copied this from a much larger file with many rows in between with irrelevant code.</p>