excel - #Value error while accessing user defined function in VBA -


code works fine when access function vba when call same function in excel cell (postalcode("23.0776120,72.6538530"), #value error. code is:

function postalcode(latlng string) string  dim xmldoc msxml2.domdocument60 dim xempdetails msxml2.ixmldomnode dim xparent msxml2.ixmldomnode dim xchild msxml2.ixmldomnode dim col, row integer  set xmldoc = new msxml2.domdocument60 xmldoc.async = false xmldoc.validateonparse = false ' use xml string create dom, on error show error message if not xmldoc.load("https://maps.googleapis.com/maps/api/geocode/xml?latlng=" & latlng)     err.raise xmldoc.parseerror.errorcode, , xmldoc.parseerror.reason end if  set xempdetails = xmldoc.documentelement set xparent = xempdetails.firstchild  row = 1 col = 1  dim xmlnodelist ixmldomnodelist  set xmlnodelist = xmldoc.selectnodes("//formatted_address")   worksheets("sheet1").cells(1, 6).value = xmlnodelist.item(0).text  dim xyz string  postalcode = xmlnodelist.item(0).text ' postalcode = "not found (try again, may have done many fast)" msgbox postalcode  end function 

it's documented limitation of user-defined functions cannot operate on or manipulate range/worksheet objects within udf called worksheet. while can value/property queries, can't change environment:

a user-defined function called formula in worksheet cell cannot change environment of microsoft excel. means such function cannot of following:

  • insert, delete, or format cells on spreadsheet.
  • change cell's value.
  • move, rename, delete, or add sheets workbook.
  • change of environment options, such calculation mode or screen views.
  • add names workbook. set properties or execute methods.

i suspect stepping through code using f8 key in vbe identify error, worksheets("sheet1").cells(1,6).value.

the basic rationale behind limitation prevent infinite loops/circular references.

there ways circumvent limitation.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -