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
SolvedPrivate

Extraction from XML file

Posted on 2015-02-11
16
42 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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