Basic Web Query in Excel - Import Data from the Web into Excel
This macro, as it currently is, will import the stock price and other similar data for Google. That information will be up to date for the second after you import the data but a refresh rate has not been setup for this macro.
To use this macro you only need to adjust three different sections. Everything else is optional and does not need to be changed but has been left in the macro in case you want to change some of the easier to understand features of web query macros. Where you see this url http://finance.yahoo.com/q?s=goog, replace that with the url of the web page from which you want to import data or information. Where you see $A$1 after where is says Destination and Range, replace that range reference with the cell where you want the data to start importing into Excel. Lastly, replace the numbers within the quotation marks from this line .WebTables = "1,2" to represent the number of the table from which you want to import data. This is a little confusing but this parameter needs to know what number table to pull in. That means that if there are 10 html tables within the web page and you want to import data that comes from the 5th table on the webpage, you would put a 5 between the quotation marks. Alternatively, if the table has a name, you can put the name of the table. But, if you do that, make sure that you wrap each table's name with an extra 2 pairs of quotation marks - the final result would look something like this .WebTables = """table1"",""table2""".
Where to install the macro: Module
Excel Macro to Perform a Basic Web Query in Excel and Import Data from the Web into Excel
With ActiveSheet.QueryTables.Add(Connection:= _
.Name = "q?s=goog_2"
.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 = "1,2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.