Code works fine when I access function from VBA however when I call the same function in excel cell (postalcode("23.0776120,72.6538530")
, I get #Value error. My code is:
Function PostalCode(latlng As String) As String
Dim xmlDoc As MSXML2.DOMDocument60
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xParent As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim Col, Row As Integer
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
' use XML string to create a DOM, on error show error message
If Not xmlDoc.Load("https://maps.googleapis.com/maps/api/geocode/xml?latlng=" & latlng) Then
Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set xEmpDetails = xmlDoc.DocumentElement
Set xParent = xEmpDetails.FirstChild
Row = 1
Col = 1
Dim xmlNodeList As IXMLDOMNodeList
Set xmlNodeList = xmlDoc.SelectNodes("//formatted_address")
Worksheets("Sheet1").Cells(1, 6).Value = xmlNodeList.Item(0).Text
Dim xyz As String
PostalCode = xmlNodeList.Item(0).Text
' PostalCode = "Not Found (try again, you may have done too many too fast)"
MsgBox PostalCode
End Function