?
SolvedPrivate

Extraction from XML file

Posted on 2015-02-11
16
Medium Priority
?
50 Views
Last Modified: 2016-02-10
I have a folder full of XML files and i want a VBA script that pulls all the data out of the XML File and put in a XLSX File and the data need to be in the correct column headings
TEST.xml
Book2.xlsx
0
Comment
Question by:Iconica
  • 7
  • 5
13 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 2000 total points
ID: 40605001
This should do the trick:

Sub ImportXMLFiles()
    Dim vFileNames As Variant
    Dim sPath As String
    Dim lFileCt As Long
    vFileNames = Application.GetOpenFilename("XML files (*.xml),*.xml", , "Please select the file(s) to import", , True)
    If TypeName(vFileNames) = "Boolean" Then Exit Sub
    
    For lFileCt = 1 To UBound(vFileNames)
        RefreshAndCopy CStr(vFileNames(lFileCt))
    Next
End Sub


Sub RefreshAndCopy(sFileName As String)
    Dim oSh As Worksheet
    
    With Worksheets("Sheet1").ListObjects(1).XmlMap
        .DataBinding.LoadSettings sFileName
        .DataBinding.Refresh
        .WorkbookConnection.Ranges(1).ListObject.DataBodyRange.Copy _
            Destination:=Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count).End(xlUp).Offset(1)
    End With
End Sub

Open in new window

0
 

Assisted Solution

by:Iconica
Iconica earned 0 total points
ID: 40607467
Hi aikimark

Thank you for your speedy response.

When I run the VBA code I get this error message and also it copy's the data multiple times please see the attached screenshot.

Many thanks for your help

Capture.PNGCapture1.PNG
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40607521
What is on Sheet2? (the destination of the copy) Any merged cells on that sheet?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Iconica
ID: 40607586
It doesn't look like it its the same xlsx spreadsheet that I have sent you unless i'm doing something wrong
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40607764
I did not send you a file, just VBA code?
0
 

Author Comment

by:Iconica
ID: 40607816
Sorry what I ment was its the same Document im working of from the one that is attached to the original question I attached 2 files an xml and a xlsx  file and I have taken the VBA code and inserted this in excel and try to run the code and I selected the file I want to import then it copy's the data multiple times then errors
0
 
LVL 11

Assisted Solution

by:jkpieterse
jkpieterse earned 2000 total points
ID: 40607918
I'm sorry, there was a tiny error in my code.
This line

        .WorkbookConnection.Ranges(1).ListObject.DataBodyRange.Copy _
            Destination:=Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count).End(xlUp).Offset(1)

Open in new window


needs to be replaced with this one:

        .WorkbookConnection.Ranges(1).ListObject.DataBodyRange.Copy _
            Destination:=Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, 1).End(xlUp).Offset(1)

Open in new window


About the multiple times: Excel just imports what the xml contains and copies that entirely.
0
 

Author Comment

by:Iconica
ID: 40607988
Hi Again

Thank you for your help I am not getting any error messages now but what seems to be happening is that I have selected a few files to import but its not importing all of them only the last file to be imported.

if i'm importing the 1 file this is fine but if I am importing multiple files then this only imports the last file not all of the files.

Many thanks
Jon
0
 
LVL 11

Assisted Solution

by:jkpieterse
jkpieterse earned 2000 total points
ID: 40608137
Doesn't it copy the data to sheet2?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40608466
>> Excel just imports what the xml contains and copies that entirely.
Try opening one file with Excel by selecting the file type XML - see what Excel is 'importing', note that Excel is asking for a schema file, do you have one? You will see the same double rows because opening is basically the same as the XmlMap function is doing (without schema). Try opening that file with a schema file, does Excel import it now as expected?
0
 

Author Comment

by:Iconica
ID: 40618390
This is working fine now thanks for helping me out with this issue

Many Thanks
John
0
 

Author Comment

by:Iconica
ID: 40618574
I've requested that this question be closed as follows:

Accepted answer: 0 points for Iconica's comment #a40618390

for the following reason:

Great solution you need to copy the VB code into visual basic and run the code
0
 

Author Closing Comment

by:Iconica
ID: 40627761
Responded very quickly and the solution is really good would definitely recommend
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question