title: Run time error, but only in the second loop
body: <p>Long time reader, first time poster. Can't stress how useful this site has been for a complete novice.</p>
  
  <p>Code below forms a URL (which then downloads file) by looping through a column of dates in one column (column 11) for 3 sets of rows (in column 2), </p>
  
  <p>i.e</p>
  
  <p>download file with URL = row1.date1, 
  then row1.date2, 
  then row1.date3. 
  Then, row2.date1, 
  then row2.date2, 
  then row2.date3. 
  Then, row3.date1, 
  then row3.date2, 
  then row3.date3. </p>
  
  <p>It completes row1.date1, then row1.date2, then row1.date3, just fine. The when it loops and starts row2, just before it downloads row2.date1, it produces run-time error '3001' at oStream.Write WinHttpReq.responseBody
  The error is: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. </p>
  
  <p>I've spent the whole weekend tryng to figure this, with no luck. Please make me look stupid by solving! I've searched, and no one seems to have the problem where connection is fine first time around in the loop, and not so, the second. Please send me link if I have missed this.</p>
  
  <pre><code>  Sub download_file()
    Dim myURL As String
    Dim y As Integer
    Dim row As Integer
  
    row = 1
  
    Do
      y = 1
  
      Do
        myURL = "XXXXXX" &amp; Cells(row, 2) &amp; "XXXXXX" &amp; Cells(y, 11)
        Dim WinHttpReq As Object
        Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
        WinHttpReq.Open "GET", myURL, False
        WinHttpReq.send
        myURL = WinHttpReq.responseBody
  
        If WinHttpReq.Status = 200 Then
          Set oStream = CreateObject("ADODB.Stream")
          oStream.Open
          oStream.Type = 1 
          oStream.Write WinHttpReq.responseBody
          oStream.SaveToFile ("Z:\XXXX\" &amp; Cells(row, 3) &amp; Cells(y, 11) &amp; ".txt.gz")
          oStream.Close
        End If
  
        y = y + 1
      Loop Until Len(Cells(y, 11)) = 0
  
      row = row + 1
    Loop Until Len(Cells(row, 2)) = 0
  End Sub
  </code></pre>
  
  <p>EDIT: @Cilla
  Fantastic! Your code has been far smoother for me, thanks! I now have to combine 2 codes, in your format. What do you think of this below? Would you do it this way?:</p>
  
  <p>{    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA"  (ByVal pCaller1 As Long, ByVal szURL1 As String, ByVal szFileName1 As String, ByVal dwReserved1 As Long, ByVal lpfnCB1 As Long, ByVal pCaller2 As Long, ByVal szURL2 As String, ByVal szFileName2 As String, ByVal dwReserved2 As Long, ByVal lpfnCB2 As Long) As Long</p>
  
  <p>Sub DownloadMe()
      Dim x As Integer
      Dim y As Integer</p>
  
  <pre><code>y = 1
  
  Do
  
  Dim strGetFrom1 As String, strSaveTo1 As String, strURL1, intResult As Long
  strURL1 = "AAAAA" &amp; Cells(y, 1) &amp; "BBBBB" 
  strSavePath1 = "C:\test\" &amp; Cells(y, 1) &amp; ".csv"
  myResult = URLDownloadToFile(0, strURL1, strSavePath1, 0, 0, 0, 0, 0, 0, 0)
  If intResult &lt;&gt; 0 Then MsgBox "Oops!  There was an error with iOS"
  
  y = y + 1
  
  Loop Until Len(Cells(y, 1)) = 0
  
  
  
  x = 1
  
  Do
  
  y = 1
  
  Do
  
  Dim strGetFrom2 As String, strSaveTo2 As String, strURL2, intResult As Long
  strURL2 = "MMMMM" &amp; Cells(x, 2) &amp; "NNNNN" &amp; Cells(y, 3) &amp; "PPPPP" 
  strSavePath2 = "C:\test\" &amp; (y, 3) &amp; ".csv"
  myResult = URLDownloadToFile(0, 0, 0, 0, 0, 0, strURL2, strSavePath2, 0, 0)
  If intResult &lt;&gt; 0 Then MsgBox "Oops!  There was an error with iOS"
  
  y = y + 1
  Loop Until Len(Cells(y, 3)) = 0
  
  
  x = x + 1
  Loop Until Len(Cells(x, 2)) = 0
  
  End Sub}
  </code></pre>
  
  <p>Could the private sub be defined inside sub downloadme ()?</p>
  
  <p>THANKS AGAIN!</p>