VBA - Dynamically change the URL in order to bring in different data sets

Eslava

Board Regular
Joined
Nov 20, 2007
Messages
112
I'm trying to pull the data out of various tables from a website.

The various tables designated by the year of the data.

I was trying to designate a cell that would have the year of the data I want.

I keep running into an error and I'm sure it has to do with the following:
Connection:="URL;http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldYear&year=" & Sheet.Range("D7"), _
Destination:=Range("A1"))

How do I fix this?

BTW, here's the full VBA code in case this helps:

Sub GetTreasuryRates()
Sheets.Add After:=ActiveSheet
Dim qt As QueryTable
Dim ws As Worksheet
'using a worksheet variable means autocompletion works better
Set ws = ActiveSheet
'set up a table import (the URL; tells Excel that this query comes from a website)
Set qt = ws.QueryTables.Add( _
Connection:="URL;http://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldYear&year=" & ActiveSheet.Range("D7"), _
Destination:=Range("A1"))
qt.RefreshOnFileOpen = True
qt.Name = "RecentTreasuryRates"
qt.FieldNames = True
qt.WebSelectionType = xlSpecifiedTables
qt.WebTables = "67"
'import the data
qt.Refresh BackgroundQuery:=True
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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