So I will start by saying that I am very new to VBA. I am trying to extract data from the table on this page. I haven't acomplished much as far as the code goes so take it easy on me. I am looking for some direction on how to approach it and if it can be done, which I believe it can. If anyone is able to help guide me in the right direction that would be much appreciated.
Sub rgnbateamstats()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.navigate "https://rotogrinders.com/team-stats/nba-earned?site=draftkings"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set allRowOfData = appIE.document.getElementById("proj-stats")
Not really sure where to go from here or if I'm even on the right track.
Whilst the table layout may appear a little odd, it actually just needs a 180 in thinking. You can grab the columns by the class name and then simply loop the rows; rather than the usual looping of rows then columns.
I use a CSS class selector to grab the columns with
querySelectorAll
i.e. I target the columns by their class name. This returns anodeList
containing each of the columns. Below is an example of the first two columns (admittedly abbr is not visible). Each row within a column is in adiv
so if I loop the columns I get the rows within each column by grabbing the associateddiv
tag collection. Then I simply loop those to write out.References:
VBA > Tools > References > Microsoft Internet Controls
Or change to late bound with:
This will grab the entire table on that page.
I will say, this site uses a very strange method on setting up their tables, and it was interesting to figure out a decent way to accomplish this.
Also, another thing that you may or may not be okay with is that there are hidden columns in this table that doesn't show on the site but will show in your excel document. If you are not okay with this, you can simply remove or hide them after this code is executed - or if you're up to modifying this to prevent that from happening during execution, more power to you.
Okay, time to attempt what's going on here.
There are three sub-tables in your table. This is the best way to explain it, but this means you will first loop through each sub table with this line:
Within that loop, you will loop that sub-table's columns with this line:
rgt-col
is the class name for the column in each table - so at least that part was easy. The functiongetSubTblCols
grabs the main sub-table element class name of one of the three names of the sub table in the arraysubTbls()
.c
is your Excel column number,r
is the row number. You also user
for each of the HTML's row numbers, but it's usesbase 0
, so you have to subtract 1.Then get the cell's value using the
innerText
property of the cell, place that into your spreadsheet, then rinse and repeat.I moved your busy webpage function to a new sub,
ieBusy
. I also added the.readyState
property because as I stated in my comment that.busy
by itself is unreliable at best.Try this part for extracting first column