You can parse XML in Excel VBA without any add-in: load the file with the built-in MSXML2.DOMDocument object, point it at the nodes you want, and write the values into cells. The whole job is usually a dozen lines of code. The only prerequisite is that the Microsoft XML library that ships with Windows is present, which it is on normal Office installations.

This guide uses the DOM (Document Object Model) approach because it is the most reliable for Excel work: it builds the document in memory and lets you select nodes by tag name or path. A second approach, MSXML2.XMLHTTP, is only useful when the XML comes from a web address.

What You'll Need

  • Excel on Windows, with the VBA editor enabled (Alt+F11 opens it). On Mac, MSXML2 is not available; you would have to use a different parser, so the steps here apply to Windows.
  • An XML sample file on disk. If you don't have one yet, save the few lines below into Notepad as invoices.xml and save it with encoding set to UTF-8. This sample is used in the worked example.
  • Basic familiarity with the VBA editor: inserting a module, pasting code, and stepping through with F8.
  • The Microsoft XML, v6.0 reference is strongly recommended but not strictly required, because late binding (creating the object with CreateObject) works without it.

The sample XML looks like this:

<invoices>
  <invoice id="1001">
    <customer>Acme Ltd</customer>
    <total>249.99</total>
  </invoice>
  <invoice id="1002">
    <customer>Beta Co</customer>
    <total>89.50</total>
  </invoice>
</invoices>

Step-by-Step

  1. Create a module. Press Alt+F11 to open the VBA editor, then use Insert > Module. All the code below goes in that module. Note that menu labels can differ slightly between Excel versions and languages.
  2. Add the reference (optional but useful). In the editor choose Tools > References and tick Microsoft XML, v6.0. Ticking it lets you declare the object as MSXML2.DOMDocument60 and get IntelliSense. The code below uses late binding instead, so it runs on any machine without the reference, which matters if colleagues will use the workbook.
  3. Load the file into a DOM object. Write a procedure that creates the object, switches off asynchronous loading, and calls Load with the full file path. Turning off async is essential: if loading stays asynchronous, the next line runs before the file has finished reading and you get an empty document.
  4. Check for load errors before doing anything else. Read the parseError.errorCode property. If it is not zero, show parseError.reason and exit. Nine out of ten "empty results" problems are a malformed XML file or a wrong path, and this check tells you which.
  5. Select the nodes you want. Use SelectNodes with a path such as "/invoices/invoice" to get all invoice elements, or SelectSingleNode when you expect exactly one. Both accept an XPath expression, which is where the real power is: "//invoice[@id='1001']/total" fetches one specific value.
  6. Write the values to the sheet. Loop the returned node list and fill cells row by row. Read text with .Text and attributes with .getAttribute("id"). Write numbers as numbers, using CDbl, or they land in Excel as text and totals won't add up:
  7. Sub ParseInvoices()
        Dim dom As Object, nodes As Object, n As Object
        Dim ws As Worksheet, r As Long
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set dom = CreateObject("MSXML2.DOMDocument60")
        dom.async = False
        dom.validateOnParse = True
        If Not dom.Load("C:\temp\invoices.xml") Then
            MsgBox "Parse error: " & dom.parseError.reason
            Exit Sub
        End If
        Set nodes = dom.SelectNodes("/invoices/invoice")
        r = 1
        For Each n In nodes
            ws.Cells(r, 1).Value = n.getAttribute("id")
            ws.Cells(r, 2).Value = n.SelectSingleNode("customer").Text
            ws.Cells(r, 3).Value = CDbl(n.SelectSingleNode("total").Text)
            r = r + 1
        Next n
        MsgBox nodes.Length & " invoices imported"
    End Sub
  8. Run and inspect. Press F5 to run the procedure. Then take a look at the sheet: you should see three columns and two rows, with the totals right-aligned because they are genuine numbers.
  9. Hand your workbook to other PCs. Because the code uses late binding, the recipient needs nothing extra beyond Office. If you used early binding and a colleague gets a "user-defined type not defined" error, that missing reference is the cause; switch to Dim dom As Object and CreateObject.

Common Mistakes

Leaving async on. This is the classic one. The document reports no error, but SelectNodes returns nothing because the file was still loading. Always set async = False before Load.

Passing a relative path. Load("invoices.xml") looks in the current directory, which is not always the workbook folder. Build the path with ThisWorkbook.Path & "\invoices.xml" or use an absolute one.

Forgetting namespaces. Real exports often include an xmlns declaration. Once a default namespace is present, a plain XPath like /root/item returns nothing, because the elements now live in that namespace. Either strip the namespace from the file first or register it on the document with setProperty "SelectionNamespaces", and prefix the node names in your XPath.

Treating numbers as text. XML holds everything as text, so be deliberate: CDbl for amounts, CLng for whole numbers. Watch decimal separators if the file was produced in a different locale.

Ignoring case and structure. XPath is case-sensitive, so Customer won't match customer. Also don't assume a file has a root wrapper: some exports use repeated top-level records, in which case SelectNodes("//record") is safer.

Tips

Use //element to search at any depth when you don't know the nesting, but prefer explicit paths for speed in large files. When you need only a handful of values, SelectSingleNode in a loop beats loading the whole tree into the sheet.

For large XML files, fetch values with .Text and write into a VBA array, then dump the array to the range in one write, instead of writing cell by cell. It is dramatically faster.

If the XML comes from a web service, use CreateObject("MSXML2.XMLHTTP.6.0"), call Open and Send, and pass .responseXML straight into your node selection, rather than writing the response to disk first.

Keep the parse procedure in its own module and pass the file path as an argument. That way you can call it for a whole folder of files, and testing any single file is trivial.

Troubleshooting

"User-defined type not defined" means an early-binding reference is missing. Switch the declarations to Object and create with CreateObject, or add the reference on the target machine.

Node list is empty. Print the error code first, then test the XPath in a single line: Debug.Print dom.SelectSingleNode("/invoices") Is Nothing. If that is Nothing, either the path or a namespace is wrong. Copy the top line of the XML and match the case exactly.

Parse error on load. Open the file in a browser or Notepad and look for an unclosed tag, a stray ampersand, or a mismatch between the declared encoding and the real one. Apostrophes inside text should be fine; unescaped & or < in content is not.

It works on your machine but not a colleague's. Register-free late binding fixes most of this. A 32/64-bit mismatch is another suspect; check the Office bitness of both machines.

Decimal values look wrong. The document may use a comma as separator. Use Replace or CDbl with an explicit culture-awareness check, and confirm against the raw file.

Frequently Asked Questions

Do I need to install anything to parse XML in VBA?

No. MSXML2 ships with Windows and is available to VBA through CreateObject on any normal Excel installation, so late binding works straight away.

DOMDocument or XMLHTTP?

DOMDocument for files already on disk or already in memory. XMLHTTP only when you need to fetch the XML from a URL first. You can parse either result with the same XPath code.

Why does my XPath return nothing when the path looks correct?

Almost always a namespace. Check whether the root element has an xmlns attribute; if it does, register it with SelectionNamespaces and prefix your node names.

Does this work on Excel for Mac?

No, not the MSXML2 objects. Mac Excel lacks that library, so the same code will fail. You would need a different parser, such as a scripting bridge, or process the file elsewhere.

Can I read XML attributes as well as element text?

Yes. Use getAttribute("name") for a value, or select attributes with XPath like @id. Element content is read from .Text.

How large a file can I process?

DOM loads the whole document into memory, so very large files can be slow. For big exports, select only the nodes you need and write in bulk to an array rather than cell by cell.