Consulting

Results 1 to 3 of 3

Thread: VBA to save .xml file from selected Columns

  1. #1
    VBAX Regular
    Joined
    Oct 2016
    Posts
    8
    Location

    Question VBA to save .xml file from selected Columns

    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 !!!

  2. #2
    Sub M_snb()
      If ActiveWorkbook.XmlMaps.Count > 0 Then ActiveWorkbook.SaveAsXMLData "J:\Temp\Book4_snb.xml", ActiveWorkbook.XmlMaps(1)
    End Sub

  3. #3
    VBAX Regular
    Joined
    Oct 2016
    Posts
    8
    Location
    Quote Originally Posted by snb View Post
    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 ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •