We help IT Professionals succeed at work.

Get External Web Result into Excel

francislam
francislam asked
on
Medium Priority
304 Views
Last Modified: 2012-05-11
I am working on an Excel model which need to look for the state, city and county name from an external web site.

Taking the web site "www.geonames.org" for example.  Is it possible for me to input the zip code and country in two cells in Excel and then it will return the searched result of "Name" and "Country" back to Excel?

Francis
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Yes you can. See the attached example. It uses the wininet api to retrieve the result from the website. Then it searches the right information inside the result. The function GetInternetResult is used to get the information from the website. The function GeoNames sends the proper request to the website and filters out the Place from the results.
When you enter a country and postal on rows 2 or 3 the place will show up in column C.

Geo.xlsm
Yes, that is possible.

 web-query.xlsm
Sure this is possible, there are really 2 approaches:

1. Use a web service.
2. Use some form of IE automation to request the page, parse the html returned to extract your results.

The first approach is the best as it is specifically designed to do what you want but is reliant on the website having a web service available for you to use. The second is no more difficult to impliment but can be easily broken if the website updates their web pages.

See: http://www.excely.com/excel-vba/ie-automation.shtml

Author

Commented:
Hi Nicobo,

Is it possible to add one more function GeoCountry to show the country details in column D, i.e., the wordings including "United States, California, Los Angeles County"?

   90210 Country Details
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi akoster,

Is it possible just to show the Name and Country in the result area?  I also don't need the words "wikipedia article" and those strange characters "Bev"rli Khils,Beverly Hills,beverli hilsi,bibarihiruzu...etc.

Thanks

 90210 Country Details

Author

Commented:
Hi Nicobo,

If I input "Las Vegas", it seems it cannot show "Clark County" properly before the "/".

 Las Vegas
CERTIFIED EXPERT

Commented:
I assumed you wanted to input country and zipcode. "Las Vegas" is not a zipcode.

Your question was about pulling in data from a website. I uploaded a code example that demonstrates a technique to do this. Now it is up to you to adapt this code to your specific needs.

Author

Commented:
Hi Nicobo,

I understand "Las Vegas" is not a zipcode but the point is even if I enter "89101" as the zip code, it still cannot show "Clark County" properly as showed below:

 89101
 
 89101a
Sure, you could link the cell representing the country and the cell representing the name.

The problem with this web page is that the results for different searches are not represented in the same way.
For some (US) results, the country is placed in cell C6 while for other searches is resides in G5.

So to be safe, i'd suggest parsing the web results with a macro, more or less as Nicobo does.
It could be performed however with less and more understandable code like this :

 web-query2.xlsm
CERTIFIED EXPERT

Commented:
I'm using this page to get the result:
http://www.geonames.org/postalcode-search.html?q=89101&country=US
because it has a clearer structure and thus is easier to process.
I saw the site has a webservice also:
http://www.geonames.org/export/web-services.html
The information retrieved from those pages is probably even easier to process.
or more reliable :
Sub websearch()
Dim ie As Object

    Application.StatusBar = "Initialising"
    Set ie = CreateObject("internetexplorer.application")
        
    Application.StatusBar = "Processing web query"
    ie.navigate "http://www.geonames.org/search.html?q=" & Range("B1") & "&country=" & Range("B2")
    
    Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
       
    Application.StatusBar = "Parsing results"
    For Each element In ie.document.getelementsbytagname("table")
        If element.classname = "restable" Then
            pos_name = -1
            pos_countru = -1
            
            '-- determine result fields
            For x = 0 To element.Children.Length - 1
                For y = 0 To element.Children(x).Children.Length - 1
                    For Z = 0 To element.Children(x).Children(y).Children.Length - 1
                        Debug.Print x & "." & y & "." & Z & " => " & element.Children(x).Children(y).Children(Z).innerhtml
                        If element.Children(x).Children(y).Children(Z).innertext = "Name" Or element.Children(x).Children(y).Children(Z).innertext = "Place" Then
                            name_x = x
                            name_y = y
                            name_z = Z
                        ElseIf element.Children(x).Children(y).Children(Z).innertext = "Country" Then
                            country_x = x
                            country_y = y
                            country_z = Z
                        End If
                    Next Z
                Next y
                If name_x > 0 And country_x > 0 Then Exit For
            Next x
                       
            '-- determine result values
            If element.Children(name_x).Children(name_y + 1).Children(name_z).Children.Length > 0 Then
                Range("B4") = element.Children(name_x).Children(name_y + 1).Children(name_z).FirstChild.innertext
            Else
                Range("B4") = element.Children(name_x).Children(name_y + 1).Children(name_z).innertext
            End If
            
            If element.Children(country_x).Children(country_y + 1).Children(country_z).Children.Length > 0 Then
                Range("B5") = element.Children(country_x).Children(country_y + 1).Children(country_z).FirstChild.innertext
            Else
                Range("B5") = element.Children(country_x).Children(country_y + 1).Children(country_z).innertext
            End If
        End If
    Next element
    If Not ie.Visible Then
        ie.Quit
        Set ie = Nothing
    End If
End Sub

Open in new window

Author

Commented:
Hi Nicobo,

I see and I guess this may be the problem as I can only see "Clark" under Admin 2 at the postalcode-serach.html.  Seem the main page does provide a more complete information than the postalcode search page.

 89101b

Author

Commented:
Hi akoster,

Any way to show also "Nevada" and "Clark County" in your search result? Either in one cell or in different cells or rows will be fine.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
As Nicobo is the first one who suggest parsing the web results with a macro, I have allocated 100 points to him.  Hope akoster is OK with this.
excellent !
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.