+ Reply to Thread
Results 1 to 17 of 17

Exctract specific values from webpages

  1. #1
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Exctract specific values from webpages

    Hi again guys,

    I’ve been struggling for a couple of days with this vba/html problem.

    The macro is supposed to enter a site which requires username and password, enter the info and redirect to a URL containing “my portfolio”.

    At this pint everything goes smoothly, but now I want to extract specific stock prices based on the HTML-code.

    As you can see from the uploaded picture I have 5 different stocks in the portfolio, each stock with different id; “tr1”-“tr5”. Normally when I’m extracting info from webpages the “class” is unique but here the class is “kursPlus” for each stock. I have highlighted stock number 1 in the picture and the stock price is 3,01 (in the grey area).

    nordnet.PNG

    So, my question is, how can I extract each stock price and print them in different cells in excel?

    This is my code so far;
    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    Can't you use the td elements of the tr element?

    Or even use the table's Rows and Cells properties?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Thanks for you imidiate response, Norie.

    I'm kind of new to coding (especially HTML) so your answer might very well be the solution, but I cannot convert your answer into VBA-code.

    I've tried to use getElementsByClassId, but can't get it to work.

    Do you mind giving an example code?

    Thomas

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    To get the tr1, tr2 etc table row elements try using getElementById.

    Once you have a row element you can loop through the cells in the row to get all the values, or you can get a specific cell value using it's (zero-based) index.

    So something like this perhaps, untested though.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Yeah, I've tried to use getElementbyID, but the same error message returns (Run-time error '438': Object doesn't support this property or method).

    Thomas

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    What exactly did you try?

  7. #7
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Well I, just replaced my "dd" with yours.

    Please Login or Register  to view this content.
    Thomas

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Exctract specific values from webpages

    Range("A1") = IE.document.getElementsByTagName("td")(5).innerText
    Range("B1") = IE.document.getElementsByTagName("td")(6).innerText
    Range("C1") = IE.document.getElementsByTagName("td")(8).innerText

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    I might have made that a bit too complicated.

    What happens if you try this.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Thank you, Norie! That really did the trick.

    Here is my final code, is there a way to make it shorter/better?

    Please Login or Register  to view this content.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    Glad that worked.

    Strange the code in post #4 didn't work as it's basically the same - perhaps trying to do too much in one go.

  12. #12
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Actually it's working fine with post #4 as well.

    It looks like the crucial point is the time delay.

    Please Login or Register  to view this content.
    If I remove any of those I just get "Run-time error '424': Object required". You know why?

    Learning by doing! Anyway, I really appriciate your help. Thank you.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    Timing is always a problem when trying to get data from webpages, that's why you'll often see code like this,
    Please Login or Register  to view this content.
    which is used to 'pause' the code until the page is ready.

    Actually instead of using Application.Wait with a fixed interval you might want to try using the above loop.

  14. #14
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Cool, that worked even better. Thanks again!

    One last thing;

    The numbers I exctract from the webpage is stored in excel as text (I guess this comes from .innerText). Is there a way to store the numbers as number (I've tried .values instead of .innertext)?

    Or do i need to format the numbers using f.eks 1 * multiply after I've extracted them?

    Thomas

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Exctract specific values from webpages

    Thomas

    You could use one of VBA's conversion functions eg Val, CLng, CDbl etc.

    Multiplying by 1 should also work.

  16. #16
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    Thank you again. I've finally managed to get the right format in each column.

    However, there is one more value I need to extract from the webpage. The value is marked with the grey line (0,29%).

    nordnet.PNG

    I've tried the following code (among with ALOT of other variants), but i can't get it to work. I guess I need to read up on HTML, but that has to be another time.

    Please Login or Register  to view this content.
    Can you give me yet another valuable tip on how to extract this value from the webpage?

    Thanks in advance.

    Thomas
    Attached Images Attached Images
    Last edited by Mangorni; 10-27-2016 at 08:02 AM.

  17. #17
    Registered User
    Join Date
    02-24-2016
    Location
    Oslo
    MS-Off Ver
    Office 365
    Posts
    59

    Re: Exctract specific values from webpages

    I'll try to rip up in this post, one more time. I've tried everything, but can't extract the value marked with the grey line (0,29%).

    I also have another question related to the same code (mentioned ni page 1). I really want to make the code as dynamic as possible, so when I buy (or sell) a new stock the code will notice this change.

    To do this I think i have to make a loop which goes through all the "tr-elements". However, I've tried but cannot get it to work. Is there anyone in here that has this knowledge of both VBA and HTML?

    Thanks in advance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to search through specific webpages automatically using excel VBA
    By mss90 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2016, 02:13 AM
  2. [SOLVED] Need help with a macro to search and exctract
    By petitesouris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2015, 08:04 PM
  3. [SOLVED] exctract minutes from hh:mm in hh:mm format
    By NoWhereMan2012 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 04-17-2015, 04:47 PM
  4. How to Exctract XML File data on Excel -using macros
    By shreeom17 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-25-2013, 10:28 AM
  5. Extract a Specific data from multiple webpages to excel
    By pari9485 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2013, 01:10 PM
  6. how exctract words within bracket
    By njan1982 in forum Excel General
    Replies: 1
    Last Post: 12-02-2008, 05:07 AM
  7. Exctract only some values to show in a chart
    By Skywise in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 11-13-2008, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1