PippaThePointer
New Member
- Joined
- Sep 21, 2023
- Messages
- 31
- Office Version
- 2016
- Platform
- 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
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