Excel VBA extract table from website

Very simply, how do I extract this table, using Excel VBA, into Excel?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You can use this code...

Sub Inport()
    With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.treasurydirect.gov/RI/OFNtebnd", Destination:=Range("$A$1"))
        .CommandType = 0
        .Name = "OFNtebnd"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newparadigmzAuthor Commented:
getting an "Invalid procedure call or argument" error message.

Does this work for you?

Am I missing a library reference?
try commenting out .CommandType = 0 and see if that works
newparadigmzAuthor Commented:
well, that was easy, thanks!

Can I ask, on the most basic level, what is this doing, going in and looking for a table and just grabbing it? What if there were 2 tables on the page?

Basically, the '.WebSelectionType' is where it specifies which table to retrieve.  You can set it to 'xlSpecifiedTables' then specify which tables using the '.WebTables' property.

Here is a an example
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.