Lookup customer name on IE and return address using VBA

TG2812

Board Regular
Joined
Apr 15, 2015
Messages
192
Hi,

I have a list of business names in Excel starting in cell A2 going down to A3, A4 etc. The record-set is pretty large as it contains over 100,000 rows. What I need to do is to retrieve the address for these business using IE and return the corresponding address in cell B2, B3 etc..

I'm pretty new in the coding world and was wondering if there is any VBA code you may think of I could use to perform this task automatically.

Thank you in advance for your valuable help.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi,

I found a code that looks similar to what I'm attempting to do. I acquired a google API key, however, it keeps on returning the same address in column 15. I feel I'm close to the end result but I would need your help to further understand what is wrong in the below code.

Code:
Sub myTest()
    Dim xhrRequest As XMLHTTP60
    Dim domDoc As DOMDocument60
    Dim domDoc2 As DOMDocument60
    Dim placeID As String
    Dim query As String
    Dim nodes As IXMLDOMNodeList
    Dim node As IXMLDOMNode


    Dim rng As Range, cell As Range


    Set rng = Range("L2:L100")


    For Each cell In rng
On Error Resume Next


    query = cell.Value


    Dim googleKey As String
    googleKey =


    Set xhrRequest = New XMLHTTP60


    xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/textsearch/xml?" & _
        "query=" & query & "&key=" & googleKey, False
    xhrRequest.send


    Set domDoc = New DOMDocument60
    domDoc.LoadXML xhrRequest.responseText
    
    placeID = domDoc.SelectSingleNode("//result/place_id").Text


    Set domDoc = Nothing
    Set xhrRequest = Nothing


    Set xhrRequest = New XMLHTTP60
    xhrRequest.Open "GET", "https://maps.googleapis.com/maps/api/place/details/xml?placeid=" & placeID & _
    "&key=" & googleKey, False
    xhrRequest.send


    Set domDoc = New DOMDocument60
    domDoc.LoadXML xhrRequest.responseText


    Dim output As String
    Dim s As String


    Set nodes = domDoc.SelectNodes("//result/address_component/type")
    For Each node In nodes
        s = node.Text
        If s = "street_number" Then
            cell.Offset(0, 1).Value = "Address: " & node.ParentNode.FirstChild.Text
        End If


        If s = "postal_code" Then
            cell.Offset(0, 2).Value = "Postal Code: " & node.ParentNode.FirstChild.Text
        End If
    Next node
    
    Cells(cell.Row, 15) = domDoc.SelectSingleNode("//result/formatted_address").Text & output
    
    Next cell


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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