Excel conversion to XML with VBA

Jorgi

Board Regular
Joined
Jul 7, 2021
Messages
52
Office Version
  1. 2019
Platform
  1. Windows
Dear All smart people. I'm looking for VBA code to convert data from multiple excel rows into one xml file. Ideally will be if the XML could be saved in the same folder where the excel file is located. Thank you very much for you help.

Excel data to be converted to XML
1633967609148.png


XML
1633967661149.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this macro - fill in the rest of the data-set line.
VBA Code:
Public Sub Create_XML_File()

    Dim XMLfileName As String
    Dim r As Long
    
    With ActiveWorkbook
    
        XMLfileName = .Path & "\Data.xml"
        
        Open XMLfileName For Output As #1
        Print #1, "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
        Print #1, "<data-set ....>"   'fill in the rest
        
        With .ActiveSheet
            For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
                Print #1, "  <record>"
                Print #1, "    <Type>" & .Cells(r, "A").Value & "</Type>"
                Print #1, "    <Type_Number>" & .Cells(r, "B").Value & "</Type_Number>"
                Print #1, "    <Software>" & .Cells(r, "C").Value & "</Software>"
                Print #1, "  </record>"
            Next
        End With
        
        Print #1, "</data-set>"
        Close #1
    
    End With
    
End Sub
 
Upvote 0
Solution
Try this macro - fill in the rest of the data-set line.
VBA Code:
Public Sub Create_XML_File()

    Dim XMLfileName As String
    Dim r As Long
   
    With ActiveWorkbook
   
        XMLfileName = .Path & "\Data.xml"
       
        Open XMLfileName For Output As #1
        Print #1, "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
        Print #1, "<data-set ....>"   'fill in the rest
       
        With .ActiveSheet
            For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
                Print #1, "  <record>"
                Print #1, "    <Type>" & .Cells(r, "A").Value & "</Type>"
                Print #1, "    <Type_Number>" & .Cells(r, "B").Value & "</Type_Number>"
                Print #1, "    <Software>" & .Cells(r, "C").Value & "</Software>"
                Print #1, "  </record>"
            Next
        End With
       
        Print #1, "</data-set>"
        Close #1
   
    End With
   
End Sub
That is brilliant/Perfect! Thank you so much John__w. You are my hero
 
Upvote 0
John_w how the macro will look like if there will be empty rows mixed with rows with data? The XML will be created only for the rows with data without the empty rows where is no data? Thank you

1634146022100.png
 
Upvote 0
Just put all the Print # lines for a record inside an If .Cells(r, "A").Value <> "" Then .... End If block.
 
Upvote 0
Just put all the Print # lines for a record inside an If .Cells(r, "A").Value <> "" Then .... End If block.
John_w, I did as you said but I'm not sure about the END IF statement. I was able to get XML without empty rows which is great and thank you again but still not sure where the end If statement should be placed. Replace End With with End If? Please advise. I'm not really good with VBA just trying to learn from the best. Thank you so much.

With .ActiveSheet
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
If .Cells(r, "A").Value <> "" Then Print #1, " <record>"
If .Cells(r, "A").Value <> "" Then Print #1, " <Type>" & .Cells(r, "A").Value & "</Type>"
If .Cells(r, "A").Value <> "" Then Print #1, " <Type_Number>" & .Cells(r, "B").Value & "</Type_Number>"
If .Cells(r, "A").Value <> "" Then Print #1, " <Software>" & .Cells(r, "C").Value & "</Software>"
If .Cells(r, "A").Value <> "" Then Print #1, " </record>"
Next
End With

Print #1, "</data-set>"
Close #1

End With

End Sub
 
Upvote 0
Replace End With with End If?
No. The End With ends the block of code which starts With .ActiveSheet

An If ... End If block like this:

VBA Code:
            If .Cells(r, "A").Value <> "" Then
                Print #1, "  <record>"
                Print #1, "    <Type>" & .Cells(r, "A").Value & "</Type>"
                Print #1, "    <Type_Number>" & .Cells(r, "B").Value & "</Type_Number>"
                Print #1, "    <Software>" & .Cells(r, "C").Value & "</Software>"
                Print #1, "  </record>"
            End If
 
Upvote 0
No. The End With ends the block of code which starts With .ActiveSheet

An If ... End If block like this:

VBA Code:
            If .Cells(r, "A").Value <> "" Then
                Print #1, "  <record>"
                Print #1, "    <Type>" & .Cells(r, "A").Value & "</Type>"
                Print #1, "    <Type_Number>" & .Cells(r, "B").Value & "</Type_Number>"
                Print #1, "    <Software>" & .Cells(r, "C").Value & "</Software>"
                Print #1, "  </record>"
            End If
Thank you very much John_w it works perfect :)
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top