Consulting

Results 1 to 4 of 4

Thread: Creating an xml file from each row in excel

  1. #1
    VBAX Regular
    Joined
    Jan 2010
    Posts
    13
    Location

    Creating an xml file from each row in excel

    Hi All
    As per my title I need to convert each row in excel to a single xml file in a certain folder.
    I don't how to do that and I am not sure if it is allowed from excel-vba.
    Please help me.
    thanks
    Garko

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like this. You'll need to adjust the SaveAs line to suit
    Sub Test()
        pth = "C:\VBAX\"
        Application.ScreenUpdating = False
        Set r = ActiveSheet.UsedRange
        For i = 1 To r.Rows.Count
            Set ws = Sheets.Add
            r.Rows(i).Copy ws.Cells(1, 1)
            ws.Move
            ActiveWorkbook.SaveAs Filename:=pth & "BookXX" & i & ".xlsx", FileFormat:=xlOpenXMLWorkbook
            ActiveWorkbook.Close
        Next i
        Application.ScreenUpdating = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jan 2010
    Posts
    13
    Location
    Hi Mdmackillop, Thanks for helping me. I tried your code, yet there is something that I have not understood. When I launch the code the create .xlsx files but I'd need those as .xml so I tried to adjust the code:
    ActiveWorkbook.SaveAs Filename:=pth & "BookXX" & i & ".xlsx", FileFormat:=xlOpenXMLWorkbook
    ActiveWorkbook.SaveAs Filename:=pth & "BookXX" & i & ".
    xml", FileFormat:=xlOpenXMLWorkbook
    My problem is that I also need to apply a certain structure to xml files. What I am thinking is to have column header with the tag names so I have to keep also the first line for each row. Sorry but even if I have a bit of experience with VBA don't know how to go so far with xml

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I know nothing about XML and recording the action fails for lack of XML mapping or such. See here for file formats
    If you need the top and other rows then use
    r.Rows(1).Copy ws.Cells(1, 1)
            r.Rows(i).Copy ws.Cells(2, 1)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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