excel macro to search a website and extract result

2019-09-20 08:57发布

问题:

I have a value in Sheet 1, A1. It is either a business name, or its associated business number (as the site searches by number or name). Once I have entered the business name (or number) of the business I need the details on, I want to be able to click a "search" button and have the results of the search displayed in a table with 2 columns (say sheet 1, A5:B9) with the labels in left column of table and the results in the right. the site i need to search is http://www.abr.business.gov.au/ eg. If i search for the business number 31701562618 these are the results i get (and how i need displayed in excel:

      Column A                       Column B
5   Entity name:                  AMBROSE, BENJAMIN STEPHEN
6   ABN status:                   Active from 05 Apr 2000
7   Entity type:                  Individual/Sole Trader
8   Goods & Services Tax (GST):   Registered from 01 Jul 2000
9   Main business location:       QLD 4310

回答1:

After some more tinkering i managed to get this to work. This just searches the site for the value in A1, grabs the results data and puts it into cell starting at A5. NO formatting however that is easy enough to include after the 'End With' bit.

Sub URL_Get_ABN_Query()
strSearch = Range("a1")
With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.abr.business.gov.au/SearchByABN.aspx?SearchText=" & strSearch & "&safe=active", _
Destination:=Range("a5"))

.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub