Excel Custom Function to Retrieve XML Data -
i create custom function retrieve specific data available in form of xml url link. first time dealing xml in excel, let alone vba, , struggling it.
i have been able write procedure (local currency exchange rate retrieval) 1 can run pressing custom button, runs code below:
sub fx_retrieve() dim fx string dim customdate string fx = range("fx") customdate = range("customdate") application.screenupdating = false activesheet.unprotect rows(2).clear activeworkbook.xmlimport url:= _ "http://www.cbu.uz/section/rates/widget/xml/" & fx & "/" & customdate, importmap:= _ nothing, overwrite:=true, destination:=range("c2") range("d:d").entirecolumn.autofit rows(2).horizontalalignment = xlcenter columns(6).clearcontents activesheet.protect application.screenupdating = true end sub
just in case, xml output looks this: 2015-12-29 usd 2809.98 1 hence, need formatting in code.
no problem procedure in case of single call fx rate! now, want go bit further , create function. , come several problems:
- i need extract specific child node (rate) xml output, looks this:
'
<response> <date_act>2015-12-29</date_act> <symbol>usd</symbol> <rate>2809.98</rate> <size>1</size> </response>
i have found similar (as in here: excel vba getting specific node xml), yet not able apply in case.
- the creation of function far can see not allow same approach had in procedure.
any appreciated!
thanks analystcave.com website run @dick kusleika, have been able adapt information presented there on topic of "working xml files" problem , have resolved it. in case has same problem, here code (i hope programmatically efficient):
public function getcurrtouzs(byref curr string, byref date_param date) currency dim xdoc object dim lists dim getthirdchild dim corrdate string if len(curr) <> 3 msgbox "currency name must 3 letters long!", vbcritical + vbokonly _ , "variable error!" exit function end if 'corrects entered date required format of "yyyy-mm-dd" corrdate = year(date_param) & "-" & month(date_param) & "-" & day(date_param) set xdoc = createobject("msxml2.domdocument") xdoc.async = false: xdoc.validateonparse = false xdoc.load "http://www.cbu.uz/section/rates/widget/xml/" & curr & "/" & corrdate 'get document elements set lists = xdoc.documentelement set getthirdchild = lists.childnodes(2) getcurrtouzs = getthirdchild.text 'output of function set xdoc = nothing 'terminates xdoc object end function
Comments
Post a Comment