+ Reply to Thread
Results 1 to 4 of 4

VBA - Pulling value from webpage?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Question VBA - Pulling value from webpage?

    I'm working on a finance assignment analyzing stock returns and I want to be able to pull the website HTML code that contains the stock information from a website, preferably in a string, and parse out the price and other information. I used to know how to do this in VB6 but I have no idea how to go about it in Excel with VBA. Any ideas? Thanks!
    Last edited by tazrockon; 10-20-2011 at 10:25 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA - Pulling value from webpage?

    Hello tazrockon,

    Welcome to the Forum!

    I know two ways to get the page source (HTML text) from a site it. One it to use an API call (the quickest). The other way is to use Internet Explorer. It can also be done using XML but I am not fluent in XML.

    Parsing can be done automatically if you use a Web Query. The Web Query eliminates having to pull the text in, store it, and then write a parsing routine. It will also load the text directly to the worksheet. I would try the Web Query first.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: VBA - Pulling value from webpage?

    Thanks, Leith Ross!

    I was able to record a macro of me pulling the table I needed off the finance website through a web query and then loop the code to get the prices of all the stocks in my portfolio.

    The only issue I have is this is a (relatively) slow process, taking up to a couple seconds for each data retrieval. You mentioned that an API call would be quicker, so could you point me in the general direction of more information on this method?

    Thanks!
    VBA's going to make me rich one day.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA - Pulling value from webpage?

    Hello tazrockon,

    The API call will return the source page text. You will have to write the code to parse and load the worksheet. Here is the macro. Copy and paste this code into a VBA module in your workbook's VBA project.
    Function GetSourceText(URL As String) As String
    
      Dim Request As Object
    
        On Error Resume Next
        Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
          If Request Is Nothing Then
             Set Request = CreateObject("WinHttp.WinHttpRequest.5")
          End If
        Err.Clear
        On Error GoTo 0
    
        Request.Open "GET", URL, False
        Request.Send
        
       'When using this property in synchronous mode, the limit to the number of
       'characters it returns is approximately 2,169,895.
        GetSourceText = Request.responsetext
        
    End Function

    Example of Using the Function
        Dim PageSource As String
    
            PageSource = GetSourceText("www.google.com")

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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