First of all I would like to thank this community for the amazing website. I am completely new to the coding world and excel vba. After learning about the possibilities in excel I started my journey to automate our daily data entry that we do here at our business. I have been able over the past few weeks to put some codes together that can access a secured website and generate the report I need based on the date I enter. However now I am stuck in how to actually pull the table values into excel. I have attached a few pictures. Right now the the third and fourth
-
I'd recommend pasting your code into the body of your question - easier for us to help and thus more likely you'll get a responseRGA– RGA2019-01-23 22:47:02 +00:00Commented Jan 23, 2019 at 22:47
-
Thank you RGA I went ahead and posted my code. Thank youMohamad Ballout– Mohamad Ballout2019-01-23 22:59:56 +00:00Commented Jan 23, 2019 at 22:59
-
Matteo NNZ has a very good answer. You'll want to be reasonably familiar with HTML coding (and CSS really). You need to find something unique that identifies the data you are pulling, then you can pull it in.HMVBA– HMVBA2019-01-23 23:03:50 +00:00Commented Jan 23, 2019 at 23:03
-
please don't post pictures of code. Use the snippet tool available via edit to insert relevant html.QHarr– QHarr2019-01-23 23:35:12 +00:00Commented Jan 23, 2019 at 23:35
-
It would help if you can verify if the classnames and ids of the relevant items are dynamic or static i.e. do they change over time/with different report selections? They look like they could be dynamic. I can think of at least one sensible and flexible approach to this if you can share the html for the whole report page using pastebin.comQHarr– QHarr2019-01-23 23:39:30 +00:00Commented Jan 23, 2019 at 23:39
1 Answer
I would approach it with a function which takes in input two parameters:
- The Internet Explorer instance
- The label you want to scrape (e.g.
NET SALES)
... and returns as String the desired value.
Unfortunately I can't help much without having the real HTML and being able to test, so this is my blind attempt (get inspired, but don't expect to copy/paste and that the code will work):
Private Function getValueByLabel(ByVal ie As SHDocVw.InternetExplorer, ByVal label As String) As String
Dim listOfRows As Object: Set listOfRows = ie.document.getElementsByTagName("tr")
Dim cellsInsideARow As Object
For Each tRow In listOfRows
Set cellsInsideARow = tRow.getElementsByTagName("td")
If cellsInsideARow(2).getElementsByTagName("div")(1).innerText = label Then
getValueByLabel = cellsInsideARow(3).getElementsByTagName("div")(1).innerText
Exit Function
End If
Next tRow
getValueByLabel = "N/A"
End Function
Basically the function above is:
- Getting the list of
tr(rows) loaded into thedocument.bodyof theInternetExplorerinstance you passed through - For each of them, checking if the
.innerTextof the 3rdtd(cell) element is equal to thelabelyou want to scrape (in your caseNET SALES) - If it's the case, returns the function with the
.innerTextof the 4thtdelement - Else, returns
N/A
You would use the function by just calling it with the good parameters from your macro, for example:
Range("A1") = "NET SALES"
Range("B1") = getValueByLabel(IE, Range("A1").Value)
P.s. the reason why I did a function is that you can re-use the same code for any element being into the same table, by just passing the wished label (NET SALES rather than GROSS SALES etc.)
15 Comments
Range("B1") = getValueByLabel(IE, "NET SALES") in your macro you'll get the value of NET SALES in cell B1, if you write Range("B2") = getValueByLabel(IE, "GROSS SALES") you'll have the value of GROSS SALES in B2, etc.