EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login

VBA XML

By Ashwani JaiswalAshwani Jaiswal

Home » VBA » Blog » VBA Coding » VBA XML

VBA XML

Excel VBA XML

The full form of XML is eXtensible Markup Language which is much like an HTML file, is designed to store and transport the data from different programs. XML file stores the data which includes the splits and separators. We can create a VBA code by which we can import the data from the XML file into Excel. In order to import the data from the XML file to any other format, we need to set some rules which tells what kind of data and fields can be used in the XML file. To create a macro in VBA for the XML file, we do not mandatory need XML Notepad in our system. But if we have it will be easy for us to read the XML file for the data we need.

Although in Excel, we have a provision of importing the data of XML file but using VBA, we can read, import the XML file in Excel.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Types of Node in VBA XML

In XML, we have a variety of Nodes that constructively helps in reading and parsing the XML file into other sources such as Word or Excel. Each DocumentElement refers to some of the nodes lists below.

  • Parent Node
  • First Child
  • Last Child
  • Child Nodes
  • Next Sibling
  • Previous Sibling

All the above-mentioned nodes confirm the type as [XDoc.DocumentElement], where only Child nodes are the array type of [XDoc.DocumentElement].

In this example, we will see a very simple VBA Code to access the XML file saved in the individual system’s any location and load them into VBA. Usually, to import XML files in Excel using VBA, we have  MSXML2.DOMDocument object to allow us to transverse the data through XML structure. But this may not be used in this example.

For this, we must have an XML file in which we can create a data structure in the form of Schema. Below is the screenshot of Company schema where under that we have Employee branch with the employee details like First Name, Last Name, Contact Number, Email ID.

VBA XML Code

VBA XML Tree

Steps to Import & Add XML in Excel VBA

Below are the examples of XML in Excel by using the VBA Code.

You can download this VBA XML Excel Template here – VBA XML Excel Template

Example #1

Step 1: First go to Insert menu tab in VBA and open a Module where we will be writing the code transverse data through XML as shown below.

Insert Module

Step 2: Write the subprocedure in the name of VBA XML for the definition of the code.

Code:

Sub VBA_XML()

End Sub

VBA XML Example 1-2

Step 3: As we already discussed, for the XML file we will require Object. So, Now define a variable using DIM as String which will be used for storing the File name.

Code:

Sub VBA_XML()

Dim XMLFile As String

End Sub

VBA XML Example 1-3

Step 4: Now we will use an application Display Alerts which is used for showing the alert message if the chosen path is incorrect.

Code:

Sub VBA_XML()

Dim XMLFile As String
Application.DisplayAlerts = False

End Sub

VBA XML Example 1-4

Step 5: Now we will put the link or location where we have kept XML file and assign it to the defined variable.

Code:

Sub VBA_XML()

Dim XMLFile As String
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"

End Sub

VBA XML Example 1-5

Note: Keep the XML file in the location which is easy to access.

Step 6: As we discussed, we will get the first Object with MSXML2.DOMDoucment.

Code:

Sub VBA_XML()

Dim CusDoc As Object
Dim Base As Object
Set CusDoc = CreateObject("MSXML2.DOMDoucment")

End Sub

VBA XML Example 1-6

Step 7: To load the data of XML file in Excel, we need to open that XML file, using the name and location which we have stored in XML File variable and select the load option as Import to List as shown below.

Code:

Sub VBA_XML()

Dim XMLFile As String
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Workbooks.OpenXML Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList

End Sub

VBA XML Example 1-7

Step 8: At last, again use the Application option to display the alert as TRUE if there is any.

Code:

Sub VBA_XML()

Dim XMLFile As String
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Workbooks.OpenXML Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList
Application.DisplayAlerts = True

End Sub

VBA XML Example 1-8

Step 9: Now we will compile the written code by pressing F8 functional key and run it, if there is no error found during compilation.

We will see, the data stored in the XML file is not imported in a new workbook as shown below. We can fetch any length of data of XML file into Excel using this simple VBA Code.

VBA XML Example 1-8

Example #2

There is another way to import the data of an XML file using the VBA Code which is simple too. For this, we can have another module of we can make the changes in the same module as well.

Step 1: For this again we would require a module and there write the subprocedure in the name of VBA XML.

Code:

Sub VBA_XML2()

End Sub

VBA XML Example 2-1

Step 2: Define a variable as String where we will be storing the file location and another variable for Workbook as shown below.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook

End Sub

VBA XML Example 2-2

Step 3: Now similar to the previous example we will now use 2 Applications, one of Screen updating and other for Display Alerts as FALSE.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False

End Sub

VBA XML Example 2-3

Step 4: Now in the defined variable XMLFile, we will assign the path of the XML file.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"

End Sub

Defined Variable Example 2-4

Step 5: Similar to example-1, we will now update set the code for Opening XML file with the path defined in XMLFile variable and load the data Import to list.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Set WBook = Workbooks.OpenXML(Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList)

End Sub

VBA XML Example 2-5

Step 6: Now again to put the Display alert application as TRUE.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Set WBook = Workbooks.OpenXML(Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True

End Sub

Display Alert Application Example 2-6

Step 7: Once the data is imported we will copy that into another workbook from the selected cell A1:D1 as per the number of headers available in the XML file.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Set WBook = Workbooks.OpenXML(Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True
WBook.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(1).Range("A1:D1")

End Sub

Workbook Example 2-7

Step 8: At last close the code using Screen updating application as TRUE.

Code:

Sub VBA_XML2()

Dim XMLFile As String
Dim WBook As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
XMLFile = "C:\Ashwani\Company.xml"
Set WBook = Workbooks.OpenXML(Filename:=XMLFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True
WBook.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(1).Range("A1:D1")
Application.ScreenUpdating = True

End Sub

Screen updating application Example 2-8

Step 9: Now if we run this code, we will see the data from the XML file will get imported into a new Excel workbook as shown below.

VBA XML Example 2-9

Pros of VBA XML

  • Even if you do not know how to create an XML file and work on it, using such VBA codes, we can extract the data into VBA Code or in Excel.
  • We can parse a portion of XML Data or complete data into the VBA window by using node references.

Things to Remember

  • Every node reference has its own value and meaning. Using the right note type is very important for every condition.
  • XML to VBA is not limited to the code which we have seen in the above examples.
  • We can extract any type of data using the code which we have seen in above-discussed examples. But keep the proper location and path which is easy to access.
  • We can also extract the XML file data without opening it from the developer tab’s Source option. This will help us to get the data as it is into the Excel workbook.
  • Save the written code Excel file in Macro enabled excel format, to avoid losing the code.

Recommended Articles

This is a guide to the VBA XML. Here we discuss Steps to Import & Add XML in Excel VBA and its different types of node along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA LBound
  2. VBA Get Cell Value
  3. VBA IsError
  4. VBA Solver
0 Shares
Share
Tweet
Share
Primary Sidebar
VBA Coding
  • VBA Coding
    • VBA Set Range
    • VBA ByVal
    • VBA Long
    • VBA Data Types
    • VBA Enum
    • VBA Clear Contents
    • VBA Call Sub
    • VBA Exit Sub
    • VBA END
    • VBA Class Module
    • VBA excel programming
    • VBA Code
    • VBA Option Explicit
    • VBA Global Variables
    • VBA Variable Declaration
    • VBA SUB
    • VBA ByRef
    • VBA Variant
    • VBA Public Variable
    • VBA Variable Types
    • VBA Goal Seek
    • VBA Variable Range
    • VBA Class Module
    • VBA XLUP
    • VBA XML
    • VBA ByVal
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Independence Day Offer - Become a VBA Developer Learn More