VBA to import multilpe xml files

PippaThePointer

New Member
Joined
Sep 21, 2023
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have the code below that works well to allow me to import a single xml file into an existing table with mapping. this brings in only the cells i want into the right spot. Perfect.
I want to do the same thing but select multiple files, or even better select all content of a folder. At this stage i dont want to use power query (will do in the future). The import xml feature allow me to select multiple files when done manualy.

Sub Import_XML()
' Select the XML file
Fname = Application.GetOpenFilename(FileFilter:="xml files (*.xml), *.xml", MultiSelect:=True)
' Check if a file is selected
If Fname = False Then Exit Sub
' Import selected XML file into existing, custom mapping
Range("Table2").Select
ActiveWorkbook.XmlMaps("VendaML_Map").Import Fname
End Sub

I found this similar code to trial but this uses a loop and pulls in all the data rather than mapping it. When i use the import/map it allows you to select multiple files.

Sub Batch_Import_XML_Files()
Dim FolderPath As String
Dim FileName As String
Dim wb As Workbook
Dim ws As Worksheet

' Specify the folder path containing XML files
FolderPath = "\"
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your desired sheet

' Loop through XML files in the folder
FileName = Dir(FolderPath & "*.xml")
Do While FileName <> ""
Set wb = Workbooks.OpenXML(FileName:=FolderPath & FileName, LoadOption:=xlXmlLoadImportToList)
wb.Sheets(1).UsedRange.Copy ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0)
wb.Close False
FileName = Dir
Loop
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
If i record macro doing it manually this is what i get below. So im wonding if anyone can help me emulate this with a loop through or something that finds all xml in the specificed folder to achieve this.

ActiveWorkbook.XmlMaps("VendaML_Map").Import URL:= _
"\\hprmel01\PTTransfer\BluePrintFD\FileA.xml"
ActiveWorkbook.XmlMaps("VendaML_Map").Import URL:= _
"\\hprmel01\PTTransfer\BluePrintFD\FileB.xml"
ActiveWorkbook.XmlMaps("VendaML_Map").Import URL:= _
"\\hprmel01\PTTransfer\BluePrintFD\FileC.xml"
ActiveWorkbook.XmlMaps("VendaML_Map").Import URL:= _
"\\hprmel01\PTTransfer\BluePrintFD\FileD.xml"
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,513
Members
452,650
Latest member
Tinfish

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