title: VBA EXCEL error 91 object variable or with block variable not set
body: <p>Hi I have this weird problem where I get error 91 ("object variable or with block variable not set"). I do not always get the error when I run the script, only now and then (random). I have tried to do as much as I can without success.</p>
  
  <p>I am scraping a site to get the most recent prices of some pc components. </p>
  
  <p>In order to do that I made a script and call the script sequentially multiple times for different parts. The error is not related to a specific part. I get the error randomly on all the parts. Sometimes I can do all the calls without any errors.</p>
  
  <p>The script (not the full script, only what you need):</p>
  
  <pre><code>Sub ImportUrlData(url As String)
  
      Dim ie As InternetExplorer
      Dim html As HTMLDocument
      Dim varListingIDElement As IHTMLElement
      Dim varShopName As String
      varShopName = ""
  
      Set ie = New InternetExplorer
      ie.Visible = False
      ie.navigate url
  
      Do While ie.readyState &lt;&gt; READYSTATE_COMPLETE
          DoEvents
      Loop
  
      Set html = ie.document
  
      Set varListingIDElement = html.getElementById("listing")
      varShopName = getXPathElement("/table/tbody/tr[1]/td[1]/p/a", varListingIDElement).innerText
  </code></pre>
  
  <p>I always get the error on this last line:</p>
  
  <pre><code>varShopName = getXPathElement("/table/tbody/tr[" &amp; count &amp; "]/td[1]/p/a", varListingIDElement).innerText
  </code></pre>
  
  <p>(The error is not occurring inside the 'getXPathElement' function, so no need to show that too. If you do want to take a look at it just ask and I'll post it here in an edit.)</p>
  
  <p>When I debug from start to end with F8 I never get the error. Is it possible that the next call already starts before the previous one isn't finished yet?</p>
  
  <p>Thanks in advance!</p>
  
  <p>(Sorry if there are some dutch words I have overseen.)</p>