mardi 19 mai 2020

Is there a way to select certain elements within an element collection in VBA

I'm still new to VBA and data scraping. I would like to check if there is a way to select certain elements within an element collection. My VBA code is as follows:

Sub Refresh()

Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLTable As MSHTML.IHTMLElementCollection
Dim Price As Variant, Change As Variant, Volume As Variant
Dim HTMLTag As MSHTML.IHTMLElement

XMLPage.Open "GET", Mapping.Range("B2"), False
XMLPage.send
HTMLDoc.body.innerHTML = XMLPage.responseText

Set HTMLTable = HTMLDoc.getElementsByClassName("stockinfocol1")

For Each HTMLTag In HTMLTable
Debug.Print HTMLTag.innerText
Next HTMLTag
End Sub

The results from debug.print is as follows:

Last Traded Share Price 1.200

Change -0.030 / -2.44%

Volume Traded 1,291,800

Day Range 1.190 - 1.250

Last Traded Date 2020-05-19

Remarks

However, I only need certain elements within this collection (eg: Last Traded Share Price and Volume Traded). Is there a way to do an For each and IF statement to sieve the elements that I want?

HTML code is here for your reference

Blockquote

<div class="stockinfocol1"><div title="AIMS APAC REIT Share Price" class="stockinfocol1row1"><span class="label">Last Traded Share Price </span><span class="value red">1.200</span></div><div class="stockinfocol1row"><span class="label">Change </span><span class="value red" label="change">-0.030 / -2.44%</span></div><div title="AIMS APAC REIT Volume Traded" class="stockinfocol1row"><span class="label">Volume Traded </span><span class="value" label="volume">1,218,700</span></div><div title="AIMS APAC REIT Day Price Range" class="stockinfocol1row"><span class="label">Day Range </span><span class="value" label="Day Range">1.190 - 1.250</span></div><div title="AIMS APAC REIT Last Traded Date" class="stockinfocol1row"><span class="label">Last Traded Date </span><span class="value" label="Last Traded Date">2020-05-19</span></div><div title="AIMS APAC REIT Remarks" class="stockinfocol1row"><span class="label">Remarks </span><span class="value" label="Remarks"> </span></div></div>

Aucun commentaire:

Enregistrer un commentaire