title: Extracting span values from within a class, VBA
body: <p>After plenty of searching, I'm struggling to scrape the data from the HTML below using VBA. Specifically, I am trying to pull the values 'DATA ONE' and 'DATA THREE' from each class="_Xnb _QJ" in the HTML code below:</p>
  
  <pre><code>&lt;div class="results"&gt;
    &lt;div class="_s2 _wPc"&gt;
      &lt;div class="_fW _QJ"&gt;
      &lt;div class="_Xnb _QJ _Z9b"&gt;
      &lt;div class="_Xnb _QJ"&gt;
      &lt;div class="_Xnb _QJ"&gt;
      &lt;div class="_Xnb _QJ"&gt;
        &lt;a href="//Extracted URL//"&gt;
          &lt;span class="_fbb"&gt;
            &lt;img id="uid_3" //Extracted// &gt;
          &lt;/span&gt;
          &lt;span class="_PHb"&gt;
            &lt;span class="_MHb"&gt;DATA ONE&lt;/span&gt;
          &lt;/span&gt;
          &lt;span class="_B6e"&gt;
            &lt;span class="_x2"&gt;DATA TWO&lt;/span&gt;
            &lt;span class="_Fs"&gt; DATA THREE &lt;/span&gt;
  </code></pre>
  
  <p>I have been trying to use getElementsByClassName to get a collection of the "_Xnb _QJ" classes, and for each of these classes use getElementsByTagName to search for "_MHb" and "_FS". I can't pick out the children in numerical order as this changes between "_Xnb.." classes, but the data I require always has the same (_MHb/FS) class tag attached.</p>
  
  <p>I am a complete novice to VBA/HTML so this code has largely been assembled by editing examples elsewhere on stackoverflow. I wonder whether the fact that the classes I require are within the "href" rather than directly below the _Xnb class is the reason I cannot pull the correct data?</p>
  
  <p>Relevant part of my VBA code below - when I run it, the code seems to run fine but no data is collected.</p>
  
  <pre><code>Dim RowNumber As Long
  Dim DataOne As String
  Dim DataThree As String
  Dim QuestionList As IHTMLElementCollection
  Dim Question As IHTMLElement
  Dim QuestionFields As IHTMLElementCollection
  Dim QuestionField As IHTMLElement
  RowNumber = 1
  
  Set QuestionList = html.getElementsByClassName("_Xnb _QJ")
  
  For Each Question In QuestionList
  Set QuestionFields = Question.getElementsByTagName("SPAN")
  
  For Each QuestionField In QuestionFields
  If QuestionField.className = "_MHb" Then
  DataOne= QuestionField.innerText
  Cells(RowNumber, 1).Value = DataOne
  End If
  
  If QuestionField.className = "_Fs" Then
  DataThree = QuestionField.innerText
  Cells(RowNumber, 2).Value = DataThree
  End If
  
  Next QuestionField
  RowNumber = RowNumber + 1
  Next
  Set html = Nothing
  MsgBox "Done!"
  
  End Sub
  </code></pre>
  
  <p>Any help would be hugely appreciated.</p>
  
  <p>Many thanks</p>