?
SolvedPrivate

Extraction from XML file

Posted on 2015-02-11
16
Medium Priority
?
45 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 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
Independent Software Vendors: 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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.

764 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