Consulting

Results 1 to 8 of 8

Thread: macro to take information from. xml file and put it in. xls file

  1. #1
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location

    macro to take information from. xml file and put it in. xls file

    Hello to all
    over a week ago I made a request for some help with a macro, but so far I have no answer. Therefore I beg you if someone is able to help me with it ( because you have to change a little , but I do not know how to do it)
    First I do not know how to change what has to change in order to be elected even din file (because it is currently allows open ( only reach a certain folder) that contains the .xml file)
    The second error is that as the macro stops and does not want to copy the information and put it in my main file.
    Thank you in advance for providing help on your part.
    Really is very important that macro
    Asking did in this forum
    Sub ImportXMLData()
    Application.ScreenUpdating = False
    'declaring variables for sub
    Dim strFolder As String, strFile As String
    Dim xlWkbk As Workbook, xmlFile As Workbook, LastRow As Long
    'calling function to open folder and browse for directory
    strFolder = GetFolder
    'if folder is empty close the sub
    If strFolder = "" Then Exit Sub
    'indicates browsing for xml files in the selected directory
    strFile = Dir(strFolder & "\*.xml", vbNormal)
    'set the excel sheet as the active one
    Set xlWkbk = ActiveWorkbook
    'loop procedure for all files in folder
    While strFile <> ""
      LastRow = xlWkbk.Sheets(1).Cells.SpecialCells(xlCellTypeLastCell).Row + 1
      Set xmlFile = Workbooks.OpenXML(Filename:=strFolder & "\" & strFile, LoadOption:=xlXmlLoadImportToList)
        xmlFile.Sheets(1).UsedRange.Copy
        LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
        xlWkbk.Sheets(1).Range("A" & LastRow + 1).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False
        strFile = Dir()
    Wend
    Set xmlFile = Nothing: Set xlWkbk = Nothing
    Application.ScreenUpdating = True
    End Sub
     
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

  2. #2
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hello to all
    please if there is anything you do not understand ask me.
    Is there anyone who can help me to work this macro. Everything else works in my file only this macro can not fix it.
    Thank you very much for the responsiveness

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi there.

    the code you posted was taken from: http://www.msofficeforums.com/excel-...xml-files.html

    including a link to the original code's web page in your code will be a good practice.

    the poster is "macropod" who is also a member of this forum.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    btw, i dont understand what you are trying to do.

    reading your thread at mrexcel makes me think you want to select a single xml file to copy data from.

    is that right?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Hello mancubus
    sorry I did not post the link where I found this macro.
    I'll do it immediately, but not so as you type it and I had not come across in the display on the forum.
    Forum in which after much searching I found this macro is this: http://www.excelforum.com/excel-prog...-workbook.html
    Please excuse me again most sincerely on my part.
    On the issue of macro: Once I found it I decided to try if it works for me and the first is to - I want to pick a certain person (.xml) file, not all files in the folder (ie at the time the macro is designed I can not come to the files only to the folder in which they are located)
    The second thing is a mistake - open (.xml) file and the macro stops - ie do not transfer information in my main file.

    Thank you once again for your cooperation.

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    below works for me for a single xml file.

    ThisWorkbook refers to the workbook that contains the macro.

    Sub ImportXMLFile()
         
         Dim fName As String
         
         Application.DisplayAlerts = False
         
         fName = "D:\My Documents\MailItems.xml" 'Change full file name to suit
         Workbooks.OpenXML Filename:=fName, LoadOption:=xlXmlLoadImportToList
         
         With ActiveWorkbook
            .Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(1).Range("A1")
            .Close False
         End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Tutor
    Joined
    Sep 2012
    Location
    London
    Posts
    237
    Location
    Thank you very much, but I have only one question - how to remove exactly where to look, and put to open a standard window in which I can navigate and access the desired file?
    To remove this:
    fName = "D:\My Documents\MailItems.xml"
    And put something like (type)
    strFile = Dir(strFolder & "\*.xml", vbNormal)
    Because this file I sit on different computers in my clients and their sales drop off.

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub ImportXMLFile()
         
        Dim fName As Variant
         
        Application.DisplayAlerts = False
         
        fName = Application.GetOpenFilename(FileFilter:="XML Files (*.xml),*.xml")
        If fName = False Then
            MsgBox "Please select the xml file to open." & vbLf & _
            "Quitting..."
            Exit Sub
        End If
        
        Workbooks.OpenXML Filename:=fName, LoadOption:=xlXmlLoadImportToList
        With ActiveWorkbook
            .Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(1).Range("A1")
            .Close False
        End With
        
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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