KaranVersation

Excel2XML - Experiments with VBA

Working with VBA for transferring data from Excel sheets onto XML files, was Part-1 of my project concerning generation of apps for mobile database implementation.

Excel2XML Main

Developer Tools

For this purpose, I utilized the Developer tools of Microsoft Excel.

To enable the developer tools, you can follows the steps mentioned here.

Once, the developer tab appears on your screen, you can begin with the coding work!

My program code would work successfully with data in multiple TABs on the worksheet.

Worksheet Tabs

Now from the developer tab, you need to select the Command Button, under Insert option.

Command Button

Command Button is inserted. You can now change its properties as follows -

Button Properties

Once the necessary changes are done, do ensure that the Design Mode button is selected, and then double click on the Button you earlier inserted to get into the VBA development area!

VBA Development Area

This is where my code comes into play.

Dim x As Integer
For x = 1 To ThisWorkbook.Worksheets.Count
    ThisWorkbook.Worksheets(x).Select
    
    Dim strXML As String
    If Application.Count(Selection) = 0 Then
        FindUsedRange
    End If
    
    Set objXMLDoc = GenerateXMLDOM(Selection, "data")
    
    Dim path As String
    path = ThisWorkbook.path & "\" & ActiveSheet.Name & " (" & ThisWorkbook.Name & ")" & ".xml"
    
    objXMLDoc.Save (path)
    
Next x
    
MsgBox ("Operation Complete!!!")

Paste this code inside the sub function of the command button.

Now you need to IMPORT the .bas file of the script that I have created.

Import BAS File

This is where you need to MODIFY the code, according to the fields of your excel sheet and the required XML tags. For this you need to know basics of VBA coding.

In my case, when the Import is done, and the code runs without error, on clicking the Command Button earlier inserted, the required XML files are generated and stored in the same directory and a confirmation message is shown -

Operation Complete

XML Files Generated

You can download my .bas file from here.

If you wish to view the complete SRS, you can download the pdf from here.

Mission Successful! :)

Original post: https://karanversation.blogspot.com/2012/01/excel2xml-experiments-with-vba.html

← Back to posts