Reading data from excel table and outputting it to xml using vba macro? -
i have data in excel table in format similar 1 below.
colum1_heading column2_heading 1 b 2 c 3
i trying convert data in table xml, have same number of columns indeterminate number of rows.
the xml format this
<?xml version="1.0" encoding="utf-8"?> <root> <tag1> <tag2> - 1, b - 2, c - 3 </tag2> </tag1> </root>
so think should simple. far i've started writing code creates writeable string write new xml file, i'm pretty new vba i'm still researching bunch on internet. i've inputted header , beginning , end tags since same. question how read rows of table , write them xml in format shown above. appreciated. please let me know if need additional info.
this should started. add reference msxml2:
sub test() new msxml2.domdocument60 dim root ixmldomnode set root = .createelement("root") dim tag1 ixmldomnode set tag1 = .createelement("tag1") dim tag2 ixmldomnode set tag2 = .createelement("tag2") tag2.text = readxmlcontentfromworksheet '"a - 1,b - 2,c - 3" tag1.appendchild tag2 root.appendchild tag1 .appendchild .createprocessinginstruction("xml", "version=""1.0"" encoding=""utf-8""") set .documentelement = root '.save "test.xml" debug.print .xml end end sub
output:
<?xml version="1.0"?> <root><tag1><tag2>a - 1,b - 2,c - 3</tag2></tag1></root>
note vertical whitespace irrelevant
the readxmlcontentfromworksheet
function this:
private function readxmlcontentfromworksheet() string dim result string dim lastrow long lastrow = mydatasheet.range("a" & mydatasheet.rows.count).end(xlup).row dim currentrow long currentrow = 1 lastrow result = result & mydatasheet.cells(currentrow, 1).value & " - " _ & mydatasheet.cells(currentrow, 2).value result = iif(currentrow = lastrow, vbnullstring, ",") next readxmlcontentfromworksheet = result end function
Comments
Post a Comment