Web Query - Yahoo Finance

president89

New Member
Joined
Jul 20, 2011
Messages
2
Hi,

I found a post about this same thing except the macro posted doesn't appear to work.

I want to go to the profile section in yahoo finance and extract the "Business Summary" paragraph.

See this link as an example. http://finance.yahoo.com/q/pr?s=AAPL+Profile

If you try to use the simple web query this paragraph is not selectable.

The variable in the link is the "AAPL". So if I could enter the ticker ie AAPL into cell A:A and have the data downloaded into B:B for multiple queries that would be great.

This was the previous thread. I tried both Macros without any luck.
http://www.mrexcel.com/forum/showthread.php?t=220971
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I want to go to the profile section in yahoo finance and extract the "Business Summary" paragraph.

See this link as an example. http://finance.yahoo.com/q/pr?s=AAPL+Profile

If you try to use the simple web query this paragraph is not selectable.
Try importing the entire page, then find and copy the data you want. Do this with the Macro Recorder and you'll have the basis for further VBA code development to import multiple symbols.
 
Upvote 0
Line 115 appears to have the data I need. Recording the macro doesn't seem to work. It always puts in the same web address no matter how many times I change it via formula in excel. When recording the macro I selected the cell that contains the web address that I have populate via formula. So i think i need ot have VBA reference the cell each time, not the specific web address i recorded the macro with.
 
Upvote 0
Paste this code into a standard module in a new workbook, and run the Test macro.
Code:
Option Explicit


Public Sub Test()

    Sheets("Sheet1").Range("A1:A3").Value = WorksheetFunction.Transpose(Array("AAPL", "MSFT", "INTC"))
    Get_Yahoo_Finance_Data

End Sub


Public Sub Get_Yahoo_Finance_Data()

    Dim baseURL As String, URL As String
    Dim dataSheet As Worksheet, querySheet As Worksheet
    Dim QT As QueryTable
    Dim lastRow As Long
    Dim symbol As Range
    
    baseURL = "http://finance.yahoo.com/q/pr"       '?s=AAPL+Profile

    'Sheet containing symbols to be retrieved
    
    Set dataSheet = Worksheets("Sheet1")
    
    'Sheet containing existing web query or where a new query will be created
    
    Set querySheet = Worksheets("Sheet3")

    'Create new web query or using existing one
    
    If querySheet.QueryTables.Count = 0 Then
        Set QT = Create_WebQuery(querySheet)
    Else
        Set QT = querySheet.QueryTables(1)
    End If

    If Not QT Is Nothing Then
        
        'Request data for each symbol in column A starting at row 1
        
        With dataSheet
            lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
            For Each symbol In .Range("A1:A" & lastRow)
                URL = baseURL & "?s=" & symbol.Value & "+Profile"
                symbol.Offset(0, 1).Value = ""
                Get_Business_Summary QT, URL, symbol.Offset(0, 1)
                DoEvents
            Next
        End With
        
    Else
    
        MsgBox "Web query not created"
        
    End If
  
End Sub


Private Sub Get_Business_Summary(QT As QueryTable, sURL As String, destRange As Range)

    Dim savedErr As ErrObject
    Dim findCell As Range
        
    On Error Resume Next        'Trap possible errors from refreshing web query
    
    QT.Connection = "URL;" & sURL
    QT.Refresh BackgroundQuery:=False
    
    If Err.Number = 0 Then

        'No error occurred - find the cell containing 'Business Summary' and copy data from 2 cells below it to destination range
        
        On Error GoTo 0
        Debug.Print Now; sURL & " - Retrieved OK"
        
        Set findCell = QT.Parent.Cells.Find(What:="Business Summary", After:=QT.Parent.Range("A1"), _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
            
        If Not findCell Is Nothing Then
            findCell.Offset(2, 0).Copy destRange
        Else
            MsgBox "Business Summary not found for " & vbCr & sURL
        End If
        
    Else

        'An unexpected error occurred - tell the user
        
        Set savedErr = Err
        On Error GoTo 0
        Debug.Print Now; sURL & " - Error " & savedErr.Number & " " & savedErr.Description

        MsgBox "Weq query URL: " & sURL & vbCr & _
            "Error number " & savedErr.Number & vbCr & _
            savedErr.Description, , "Web query error"

    End If

End Sub


Private Function Create_WebQuery(querySheet As Worksheet) As QueryTable
    
    'Create web query on specified sheet.  Note that a URL is not specified in the Connection string, but instead is specified when querying each symbol
    
    Set Create_WebQuery = querySheet.QueryTables.Add(Connection:="URL;", Destination:=querySheet.Range("A1"))
     
    If Not Create_WebQuery Is Nothing Then
         With Create_WebQuery
            .Name = "Yahoo_Finance"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage    'Import entire page
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            '.Refresh BackgroundQuery:=False    'Refresh when retrieving each URL, not here
        End With
    End If
    
End Function
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,862
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top