Imposter
New Member
- Joined
- Nov 19, 2021
- Messages
- 15
- Office Version
- 2016
- Platform
- Windows
Hello,
I need to import thousands of XML files with unique filenames (from multiple level one subfolders inside main folder, i.e. xmlDownload\0001\*.xml) to template XLSM file (with customized XML Map/Scheme), apply data transformation using Power Query inside this XLSM file (using query refresh) and save transformed result sheet as a separate XLSM file for each folder (i.e. xmlDownload\0001.xlsm).
The steps required are:
1) open XLSM template file and run VBA Macro
2) folder selection dialig appears > you select folder (i.e. xmlDownload) with folders, each folder contains XML files (i.e. xmlDownload\0001\*.xml)
3) each folder's XML files are imported to "XML" sheet of template, then query on "Data" sheet is refreshed and Power Query transformations get applied to data and pasted to "Data" sheet, then "Data" sheet is copied to new workbook with name "0001.xslm" and saved in same folder as XLSM template.
Here's the code I 've got that does the following for single folder containing any number of XML files:
1) XLSM template file contains ready-made (customized) XML Map on sheet "XML" and Power Query on sheet "Data"
2) Run Macro > folder selecting dialog appears > you select folder with XML files
3) XML data is imported to "XML" sheet
4) query on "Data" sheet is refreshed and Power Query transformations get applied to data
That's as far as I could bet by myself.
And here're my files (my XLSM template and several XML files to play with) - link.
Kind regards,
Imposter
On the very last step of my project I faced a problem with my computer limitation to process data and Excel engine limitation of 1'048'576 rows. I'm here looking for help. I'm not quite familiar with VBA and quite exosted with this project (I'm helping my local organization supporting homeless people & veterans to get government support and I'm doing it all for free). I'd be really grateful if somebody could support me finishing this.
The steps required are:
1) open XLSM template file and run VBA Macro
2) folder selection dialig appears > you select folder (i.e. xmlDownload) with folders, each folder contains XML files (i.e. xmlDownload\0001\*.xml)
3) each folder's XML files are imported to "XML" sheet of template, then query on "Data" sheet is refreshed and Power Query transformations get applied to data and pasted to "Data" sheet, then "Data" sheet is copied to new workbook with name "0001.xslm" and saved in same folder as XLSM template.
At first I planned placing all files into one folder and importing from there, but it turned out a) my computer don't have enough RAM (inc. virtual memory because of insufficient disk space) to apply transformations to so much data at once; b) it takes forever; c) I can easily exceed Excel Engine limitation on rows while doing all at once. Thus I made a batch file that separates all my XML files into folders by 10000 files, i.e. xmlDownload\0001\*.xml, xmlDownload\0002\*.xml, etc.
1) XLSM template file contains ready-made (customized) XML Map on sheet "XML" and Power Query on sheet "Data"
2) Run Macro > folder selecting dialog appears > you select folder with XML files
3) XML data is imported to "XML" sheet
4) query on "Data" sheet is refreshed and Power Query transformations get applied to data
VBA Code:
Sub Import_all_XML_files_from_the_specified_folder_preserving_existing_XML_Map()
' https://www.extendoffice.com/documents/excel/3388
Application.DisplayAlerts = False
Dim xWb As Workbook
Dim xSWb As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xCount As Long
Dim xURL As String
Dim n As Long
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
Application.ScreenUpdating = False
Set xSWb = ThisWorkbook
xFile = Dir(xStrPath & "\*.xml")
Do While xFile <> ""
xURL = xStrPath & "\" & xFile
xmlImport xURL, xSWb
xFile = Dir()
Loop
' xSWb.Save
Application.ScreenUpdating = True
Exit Sub
Application.DisplayAlerts = True
End Sub
Sub xmlImport(xURL As String, Wb As Workbook)
Application.DisplayAlerts = False
Dim xMap As XmlMap
Set xMap = Wb.XmlMaps(1)
xMap.Import URL:=xURL, Overwrite:=False
Sheets("Data").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Application.DisplayAlerts = True
End Sub
Here's BAT code to separate mamy XML files into folders (xmlStack-001, etc.) by 100 file in case someone needs it:
Code:
cd /d "%~dp0\xmlDownload"
:: https://stackoverflow.com/a/2542286
set groupsize=100
set n=1
set nf=0
for %%f in (*.xml) do (
if !n!==1 (
set /a nf+=1
md xmlStack-!nf!
)
move /Y "%%f" xmlStack-!nf!
if !n!==!groupsize! (
set n=1
) else (
set /a n+=1
)
)
pause
And here're my files (my XLSM template and several XML files to play with) - link.
Kind regards,
Imposter