PDA

View Full Version : VBA to save .xml file from selected Columns



bglumac
10-18-2016, 04:01 AM
Hi guys, I need help with VBA for button to export .xml table by selection.

I tried to copy and combine some example code but with no luck.
It creates .xml but not for selection or fixed range (never mind) but the data is wrong as I don't know how to code it properly...

The code is:

Private Sub Xml_click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = ActiveWorkbook.Path & "\Popis.xml"

Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value

If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
What I need is a creation of .xml file with two columns of data from selected cells or entire table after clicking the button.

If someone could help, it would be a life saver
Thank you all in advance !!!

snb
10-18-2016, 04:34 AM
Sub M_snb()
If ActiveWorkbook.XmlMaps.Count > 0 Then ActiveWorkbook.SaveAsXMLData "J:\Temp\Book4_snb.xml", ActiveWorkbook.XmlMaps(1)
End Sub

bglumac
10-18-2016, 04:55 AM
Sub M_snb()
If ActiveWorkbook.XmlMaps.Count > 0 Then ActiveWorkbook.SaveAsXMLData "J:\Temp\Book4_snb.xml", ActiveWorkbook.XmlMaps(1)
End Sub


Sorry for my ignorance, but I added lines you suggested and changed the path but it's not working.
Can you please explain what is the function of these lines ?