0

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

15
  • 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 response Commented Jan 23, 2019 at 22:47
  • Thank you RGA I went ahead and posted my code. Thank you Commented 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. Commented Jan 23, 2019 at 23:03
  • please don't post pictures of code. Use the snippet tool available via edit to insert relevant html. Commented 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.com Commented Jan 23, 2019 at 23:39

1 Answer 1

2

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 the document.body of the InternetExplorer instance you passed through
  • For each of them, checking if the .innerText of the 3rd td (cell) element is equal to the label you want to scrape (in your case NET SALES)
  • If it's the case, returns the function with the .innerText of the 4th td element
  • 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.)

Sign up to request clarification or add additional context in comments.

15 Comments

how can I get it to enter the data into my sheet 1 cell A17? And do I need to post that code for every label I need? I am sorry I am a total noob on this stuff just trying to understand it correctly. Can you write the actual code to generate the net sales and value or is the above what I need to write in?
@MohamadBallout, I can't code something working without having real access to the code. All I can do is look at the HTML structure you posted and try my guess. In my answer you already have what you need, if you write 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.
Matteo, I understand. I copied that same code in but is not returning any value
do you do any private coding? via team view or something? @Matteo NNZ
@QHarr you’re right, I’ve fixed the issue. MohamadBallout, you might want to check the code after edit proposed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.