multiple XML in Excel via VBA without folder

JokerExecution

New Member
Joined
Mar 2, 2020
Messages
6
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hey.
I'm working with some big XML Files which I have to insert into Excel.
I have a runnig code but its only with folder selection, but I need the multiple selection of XML files.
The folders are quite big and the Files also bigger.

Here is my current code
VBA Code:
Sub From_XML_To_XL()
Dim xmlWb As Workbook, xSWb As Workbook, xStrPath$, xfdial As Variant, _
xFile$, lr%, first As Boolean, r As Range, p As PivotTable
first = True
Set xfdial = Application.GetOpenFilename("XML Dateien (*.xml),*.xml", MultiSelect:=True)
If xfdial.Show = -1 Then xStrPath = xfdial.SelectedItems(1) & ""
If xStrPath = "" Then Exit Sub
Application.ScreenUpdating = False
Set xSWb = ThisWorkbook
xSWb.Sheets(3).Cells.Clear
xSWb.Sheets(2).Activate
lr = xSWb.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row    ' last used row, column A
xFile = Dir(xStrPath & "\*.xml")
Do While xFile <> ""
    Set xmlWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
    If first Then
        Set r = xmlWb.Sheets(1).UsedRange                       ' with header
    Else
        xmlWb.Sheets(1).Activate
        Set r = ActiveSheet.UsedRange
        Set r = Range(Cells(3, 1), Cells(r.Rows.Count, r.Columns.Count))
    End If
    r.Copy xSWb.ActiveSheet.Cells(lr + 1, 1)
    lr = xSWb.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row
    xmlWb.Close False
    first = False
    xFile = Dir()
Loop
xSWb.Sheets(1).Activate
For Each p In ActiveSheet.PivotTables
    p.RefreshTable
Next p

Application.ScreenUpdating = True
xSWb.Save
MsgBox "End of code."
Exit Sub
ErrHandler:
MsgBox "Error!"
End Sub

It is really importent that the XML files inserted to one table.

Thank u
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Sorry, my initial code didn't work.

I'll think about it a bit more.
 
Upvote 0
Thank u
btw
This is the code which is working with folders

VBA Code:
Sub From_XML_To_XL()
Dim xmlWb As Workbook, xSWb As Workbook, xStrPath$, xfdial As FileDialog, _
xFile$, lr%, first As Boolean, r As Range, p As PivotTable
first = True
Set xfdial = Application.FileDialog(msoFileDialogFolderPicker)
xfdial.AllowMultiSelect = False
xfdial.Title = "Select a folder"
If xfdial.Show = -1 Then xStrPath = xfdial.SelectedItems(1) & ""
If xStrPath = "" Then Exit Sub
Application.ScreenUpdating = False
Set xSWb = ThisWorkbook
xSWb.Sheets("Tabelle").Cells.Clear
xSWb.Sheets("Tabelle").Activate
lr = xSWb.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row    ' last used row, column A
xFile = Dir(xStrPath & "\*.xml")
Do While xFile <> ""
    Set xmlWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
    If first Then
        Set r = xmlWb.Sheets(1).UsedRange                       ' with header
    Else
        xmlWb.Sheets(1).Activate
        Set r = ActiveSheet.UsedRange
        Set r = Range(Cells(3, 1), Cells(r.Rows.Count, r.Columns.Count))
    End If
    r.Copy xSWb.ActiveSheet.Cells(lr + 1, 1)
    lr = xSWb.ActiveSheet.Range("a" & Rows.Count - 1).End(xlUp).Row
    xmlWb.Close False
    first = False
    xFile = Dir()
Loop
xSWb.Sheets("Pivottabelle").Activate
For Each p In ActiveSheet.PivotTables
    p.RefreshTable
Next p

Application.ScreenUpdating = True
xSWb.Save
MsgBox "End of code."
Exit Sub
ErrHandler:
MsgBox "Error!"
End Sub
 
Upvote 0
Are you just wanting to select multiple files in a folder as opposed to the folder and find all the files?
 
Upvote 0
The first code you posted allows you to select multiple files.

xfdial returns an array, so long as you have selected something. Even 1 item will be an array which is 1 element.

You can use a For Each loop.

VBA Code:
For Each FileName in XfDial

    'Your File Processing code goes here

Next
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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