Need Help! web query macro.

gger

New Member
Joined
Jan 5, 2011
Messages
25
Hi Guys,

I am new to this so be patient. I am looking to create a button that pulls data from a website based on the content of a cell. I have attempted to use the macro recorder to copy the content of a cell, run a web query (to: http://www.financialpost.com/) and paste the copied cell value into the stock quote search box on the right hand side. I want it to select the ticker, go to financials and import the balance sheet to cell B15. Ideally, I would want it to import all the financials for that ticker (balance sheet, cash flow, income statement) and import it on the sheet with the data side by side.

I have run in to two problems:
1. I can get it to run the macro, but regardless of the content of the copied cell (the ticker), it will only return data from the ticker i used to create the macro.

2. If i click the button more than once, instead of clearing data from the earlier search it will return data next to the earlier data. I would prefer if it wiped the previous search and returned the data in B15.

Ultimately, my goal is to be able to input a ticker in a specific cell, press the "run" button, and have the income statement, cash flow statement, and balance sheet statement data pop up on my "Data" sheet.

This is my code at the moment:

Private Sub CommandButton1_Click()
'
' Macro5 Macro
' Macro recorded 05/01/2011 by gger
'

'
Range("B7").Select
ActiveCell.FormulaR1C1 = "AAPL"
Range("D14").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://idms.financialpost.com/stocks/balance_sheet.idms?SYMBOL=AAPL&ID_NOTATION=253929" _
, Destination:=Range("D14"))
.Name = "balance_sheet.idms?SYMBOL=AAPL&ID_NOTATION=253929"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """IDMS_BalanceSheet"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
Any thoughts?
Thanks in advance,

gger.
 
Yeah, I hear you on yahoo vs fp. a while back my web queries crashed on me and i found that when i tried to import the data tables from yahoo the little check-mark boxes were gone? i was not sure how to fix it so i switched my web queries to fp, which had the check-mark boxes still there.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Guys,

my latest code looks like this:

' Import Company Summary

Range("H2").Select
ActiveCell.FormulaR1C1 = "AAPL"
Range("G2").Select
Sheets("Data2").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q?s=" & Range("H2").Value & "&ql=0", Destination:=Sheets("Data2").Range("B2"))
.Name = "q?s=" & Range("H2").Value & "&ql=0"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = """table1"",""table2"""
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Balance Sheet (annual)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value & "+Balance+Sheet&annual", Destination _
:=Sheets("Data2").Range("E2"))
.Name = "bs?s=" & Range("H2").Value & "+Balance+Sheet&annual"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,8,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Balance Sheet (quarterly)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value, Destination:=Sheets("Data2").Range("J2"))
.Name = "bs?s=" & Range("H2").Value & ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Income Statement (annual)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value & +Income+Statement&annual", _ Destination:=Sheets("Data2").Range("Q2"))
.Name = "is?s=" & Range("H2").Value & "+Income+Statement&annual"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,8,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Income Statement (quarterly)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value & "", Destination:=Sheets("Data2").Range("W2"))
.Name = "is?s=" & Range("H2").Value & ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,8,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Cash Flow (annual)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/cf?s=AAPL+Cash+Flow&annual", Destination:=Sheets("Data2").Range("AD2"))
.Name = "cf?s=AAPL+Cash+Flow&annual"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,8,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

' Import Cash Flow (quarterly)

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/cf?s=AAPL", Destination:=Sheets("Data2").Range("AI2"))
.Name = "cf?s=AAPL"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "7,8,9,10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

Sheets("NetNet").Select
Range("A1").Select

Application.ScreenUpdating = True

End Sub


My issue at the moment is altering the URL code to allow for it to search based on a ticker i input to cell H2 on a previous sheet. The code provided on the earlier page no longer works as I have changed from financialpost to yahoo finance. Do you think you could let me know how i do this?

i have highlighted in green my attempts to solve this... the red highlight is where i gave up... therefore this is the original code from my macro recorder.

thanks...
 
Upvote 0
The following URLs should give you what you're looking for:

Company Summary:
Code:
"URL;http://finance.yahoo.com/q?s=" & Range("H2").Value
Balance Sheet (Annual):
Code:
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value & "&annual"
Balance Sheet (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value
Income Statement (Annual):
Code:
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value & "&annual"
Income Statement (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value
Cash Flow (Annual):
Code:
"URL;http://finance.yahoo.com/q/cf?s=" & Range("H2").Value & "&annual"
Cash Flow (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/cf?s=" & Range("H2").Value
Hope that helps!
 
Upvote 0
The following URLs should give you what you're looking for:

Company Summary:
Code:
"URL;http://finance.yahoo.com/q?s=" & Range("H2").Value
Balance Sheet (Annual):
Code:
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value & "&annual"
Balance Sheet (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/bs?s=" & Range("H2").Value
Income Statement (Annual):
Code:
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value & "&annual"
Income Statement (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/is?s=" & Range("H2").Value
Cash Flow (Annual):
Code:
"URL;http://finance.yahoo.com/q/cf?s=" & Range("H2").Value & "&annual"
Cash Flow (Quarterly):
Code:
"URL;http://finance.yahoo.com/q/cf?s=" & Range("H2").Value
Hope that helps!

you're the best. thanks!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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