SolvedPrivate

Extraction from XML file

Posted on 2015-02-11
16
35 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
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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 37

Expert Comment

by:Gerwin Jansen
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now