SolvedPrivate

Extraction from XML file

Posted on 2015-02-11
16
43 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
16 Comments
 
LVL 11

Accepted Solution

by:
jkpieterse earned 500 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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 500 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 500 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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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