Import XML workbook data

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have multiple .xml files in a folder and I need to place them in an Excel worksheet one on top of the other.

So far with the code below, it works to extract the first file, but when trying to extract the next file, I get the error...

The operation cannot be completed because the XML table is bound to a different XML map.

Code:
Sub LoopThruFolder()
    Dim MyFile As String
    Dim MyDir As String
    Dim wb As Workbook
    Dim ScrLR As Long
    Dim Rng As Range
    Dim DestLR As Long
    
    Set wb = ThisWorkbook
    
    MyDir = ThisWorkbook.Path & Application.PathSeparator
    MyFile = Dir(MyDir & "*.xml")
    ChDir MyDir
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Do While MyFile <> ""
        If MyFile <> ThisWorkbook.Name Then
        DestLR = wb.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
        wb.XmlImport URL: = MyFile, ImportMap:=Nothing, Overwrite:=True, Destination:=Range("A" & DestLR)
        End If
        MyFile = Dir()
    Loop
    Application.DisplayAlerts = True
End Sub

I've searched on-line for some tips and tutorials, but getting a little lost. Any thoughts?
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Untested, try replacing the XmlImport line with:

Code:
        If ThisWorkbook.XmlMaps.Count = 0 Then
            'Import first XML data file, creating a new XML mapping
            ThisWorkbook.XmlImport URL:=MyDir & MyFile, ImportMap:=Nothing, Overwrite:=True, Destination:=Range("A" & DestLR)
        Else
            'Append subsequent XML data files to existing mapping - don't specify Destination
            ThisWorkbook.XmlImport URL:=MyDir & MyFile, ImportMap:=Nothing, Overwrite:=False
        End If
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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