?
Solved

Get External Web Result into Excel

Posted on 2011-04-25
17
Medium Priority
?
222 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
0
Comment
Question by:francislam
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 35465535
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
0
 
LVL 19

Expert Comment

by:Arno Koster
ID: 35465542
Yes, that is possible.

 web-query.xlsm
0
 
LVL 6

Expert Comment

by:richardburwood
ID: 35465549
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:francislam
ID: 35466689
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
0
 
LVL 22

Assisted Solution

by:Nico Bontenbal
Nico Bontenbal earned 400 total points
ID: 35466859
Like this?
Geo.xlsm
0
 

Author Comment

by:francislam
ID: 35466880
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
0
 

Author Comment

by:francislam
ID: 35467046
Hi Nicobo,

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

 Las Vegas
0
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 35467229
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.
0
 

Author Comment

by:francislam
ID: 35467600
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
0
 
LVL 19

Expert Comment

by:Arno Koster
ID: 35467647
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
0
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 35467721
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.
0
 
LVL 19

Expert Comment

by:Arno Koster
ID: 35467819
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

0
 

Author Comment

by:francislam
ID: 35467830
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
0
 

Author Comment

by:francislam
ID: 35467966
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.
0
 
LVL 19

Accepted Solution

by:
Arno Koster earned 1600 total points
ID: 35468156
sure, replace this code

            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

Open in new window


with

Range("B5") = Replace(element.Children(country_x).Children(country_y + 1).Children(country_z).innertext, vbCrLf, ", ")

Open in new window

0
 

Author Closing Comment

by:francislam
ID: 35468378
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.
0
 
LVL 19

Expert Comment

by:Arno Koster
ID: 35473345
excellent !
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question