Click here to Skip to main content
13,631,070 members
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 3 May 2004
Licenced CPOL

Export Excel to XML in VBA

, 3 May 2004
Rate this:
Please Sign up or sign in to vote.
VBA to generate a clean XML file from an Excel table provided with nested nodes support
Sample Image - xls2xml.jpg


In Excel XP, there is a new export option for XML. However, what I get is many rubbish tags (I'm so disappointed...). Then, after searching on the Google (our nice search engine) for some time about "Converting Excel to XML", what I get are conversion tools that simply make a Rational table to XML. However, what I want is the support of nested structures (e.g. <data><field1>....</field1><data>). As a result, I decided to write my own using VBA... (don't ask me why VBA... may be I will port it to my favorite C++ platform later. :)

Using the Code

The source code contains 2 functions GenerateXMLDOM() and fGenerateXML(). They are actually doing the same thing in a different approach.

GenerateXMLDOM -- Gives a Microsoft XMLDOM object which you can play with or serialize into an XML at any time using the "Save" method.

fGenerateXML -- Generates a long string which is ready for output as file (more readable, as proper newline is added).

Both functions have the same parameters. The first one is rngData, which is the selected table area in the Excel Sheet for conversion. The second (rootNodeName) is the first/root node's name used in the XML.

In the selected range, the first row is expected to be the field (or node) names. One thing that is worth noticing is that we can use the node name to define the level it belongs to. Started and separated by the node delimiter "/", the node name is one level deeper than the previous one. e.g. /data/field1 is equivalent to <data><field1>....</field1><data>, /student/name/surname is equivalent to <student><name><surname>.... </surname></name></student>:

' rngData : The selected region on Excel sheet, with the first row as field name,
' and data rows below
'  For the field name, use the node delimiter "/" to build the hierarchy of data
'  e.g. /data/field1 is equivalent to <DATA><FIELD1>....</FIELD1><DATA>
' rootNodeName : The XML document root node tag name

Function GenerateXMLDOM(rngData As Range, rootNodeName As String)

Function fGenerateXML(rngData As Range, rootNodeName As String) As String

Limitation and Notes

The ordering of fields may affect the generated XML, fields that have to be placed inside the same sub-node should be placed next to each other. For example, if /student/id and /student/name are placed next to each other, it will generate:


However, if not, the result will be:

<student><id>..</id></student> <somebrabra...> ... </somebrabra...>

The reason is that it only checks with the last field instead of all before deciding where the node should be created.

Finally I would like to thank Hasler Thomas, the author of A Filebrowser for VBA, who provided the code for file browse. Hope this code will be useful for you. Please let me know if there are any bugs.


  • 4 May 2004 -- First release 0.8 version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raymond Pang
Web Developer
Hong Kong Hong Kong
I am fond of programming especially in C++ (but unluckily I know many other different lang.). My special interest is on Graphics and image processing.

After graduated from Chinese University of Hong Kong with a research degree about graphics, I starts my career on web programming and games. Please Visit which has software and game projects done by me.

You may also be interested in...

Comments and Discussions

Questionattribute in xml Pin
Member 773630414-Jul-18 22:56
memberMember 773630414-Jul-18 22:56 
SuggestionSharing macro to zip a folder Pin
Member 77363041-Jul-18 17:35
memberMember 77363041-Jul-18 17:35 
QuestionExport multiple rows in xml Pin
StefanoCadeddu17-Jul-17 6:04
memberStefanoCadeddu17-Jul-17 6:04 
QuestionHow to use? Pin
Member 1302021123-Feb-17 12:12
memberMember 1302021123-Feb-17 12:12 
QuestionNot working if I give attributes values. Pin
Member 1276393029-Sep-16 2:07
memberMember 1276393029-Sep-16 2:07 
QuestionHow to export to XML a schema with multiple child elements Pin
Member 123025616-Feb-16 6:10
memberMember 123025616-Feb-16 6:10 
SuggestionBeginner XML for VBA devs Pin
Member 1176681125-Nov-15 1:13
memberMember 1176681125-Nov-15 1:13 
SuggestionRecognise Attributes? Pin
ajingar1-Jun-15 23:55
memberajingar1-Jun-15 23:55 
GeneralRe: Recognise Attributes? Pin
ajingar18-Aug-15 1:06
memberajingar18-Aug-15 1:06 
QuestionDublin Core Pin
Member 115880958-Apr-15 2:33
memberMember 115880958-Apr-15 2:33 
Questionis it possible if i want to push a schema to this module? tia . Pin
Member 1146409120-Feb-15 2:09
memberMember 1146409120-Feb-15 2:09 
QuestionRoot Node Pin
Member 1108865717-Sep-14 0:22
memberMember 1108865717-Sep-14 0:22 
Questionfor merged cells Pin
Member 969485011-Aug-14 3:20
memberMember 969485011-Aug-14 3:20 
QuestionNot working in Excel 2011 mac Pin
Bharath L Narasimman8-Jul-14 1:46
memberBharath L Narasimman8-Jul-14 1:46 
GeneralMy vote of 5 Pin
Member 1082337716-May-14 2:49
memberMember 1082337716-May-14 2:49 
QuestionExport excel to XML Pin
kothai p6-Mar-14 1:34
memberkothai p6-Mar-14 1:34 
QuestionFormat of the hierarchy of data in the xml file Pin
Phil Gilbert20-Jun-13 23:52
memberPhil Gilbert20-Jun-13 23:52 
QuestionThanks! Great script! Pin
Vladislav Gasan3-Jan-13 0:35
memberVladislav Gasan3-Jan-13 0:35 
QuestionNeed vba code for xml to excel Pin
syed mubeen ahmed23-Jun-12 9:59
membersyed mubeen ahmed23-Jun-12 9:59 
xml to excle
Questionxml to excel Pin
syed mubeen ahmed23-Jun-12 9:47
membersyed mubeen ahmed23-Jun-12 9:47 
QuestionNeed help creating repeating group nodes using this tool Pin
Dan Gruber1-Sep-11 5:12
memberDan Gruber1-Sep-11 5:12 
AnswerRe: Need help creating repeating group nodes using this tool Pin
Member 138830129-Jul-18 2:57
memberMember 138830129-Jul-18 2:57 
Questionloop through all worksheets in a workbook t Pin
ushasharma198425-Jul-11 0:08
memberushasharma198425-Jul-11 0:08 
GeneralStill helpful in 2011 Pin
Eelze13-Apr-11 23:28
memberEelze13-Apr-11 23:28 
GeneralEXCEL HELP!!!! Pin
MRMIG14-Jul-10 20:09
memberMRMIG14-Jul-10 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180712.1 | Last Updated 4 May 2004
Article Copyright 2004 by Raymond Pang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid