Solved

How do I Transpose XML Rows to a Single Column using "<" as the New Row Return

Posted on 2013-12-01
9
425 Views
Last Modified: 2013-12-02
From This
<STMTTRN><TRNTYPE>DEBIT<DTPOSTED>20120701<TRNAMT>-80<FITID>01JUL2012.1<MEMO>Transaction1</STMTTRN>
<STMTTRN><TRNTYPE>CREDIT<DTPOSTED>20120702<TRNAMT>280<FITID>01JUL2012.2<MEMO>Transaction2</STMTTRN>

Open in new window

To This.  Please note rows must format with no row spaces
<STMTTRN>
<TRNTYPE>DEBIT
<DTPOSTED>20120701
<TRNAMT>-80
<FITID>01JUL2012.1
<MEMO>Transaction1
</STMTTRN>
<STMTTRN>
<TRNTYPE>CREDIT
<DTPOSTED>20120701
<TRNAMT>280
<FITID>01JUL2012.2
<MEMO>Transaction2
</STMTTRN>

Open in new window

0
Comment
Question by:XGIS
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:duncanb7
Comment Utility
Why your code is no closing tag in XML format ?

And what do you want for transpose ?

You want the tranpose output on Excel or Website ?

If display xml doc on website, just save the following as doc.xml and run it
on website ? and it can be displayed on Excel too but not at such your format

Hope you can clarify your question more or clearly

Duncan

<?xml version="1.0" encoding="UTF-8"?>
<note>
<STMTTRN><TRNTYPE>CREDIT</TRNTYPE><DTPOSTED>20120702</DTPOSTED><TRNAMT>280</TRNAMT><FITID>01JUL2012.2</FITID><MEMO>Transaction2</MEMO></STMTTRN>
<STMTTRN><TRNTYPE>DEBIT</TRNTYPE><DTPOSTED>20120701</DTPOSTED><TRNAMT>-80</TRNAMT><FITID>01JUL2012.1</FITID><MEMO>Transaction1</MEMO></STMTTRN>
</note>

Open in new window

0
 
LVL 7

Author Comment

by:XGIS
Comment Utility
Pls see the attached spreadsheet.  The pasted BOLD values are static. This is the required output. Every time it sees a "<" the XML field starts a new row. It would be beneficial if it did it on the next sheet as it will obviously be quite long depending on how many rows I generate.
This sample is designed to generate testing data for a C# program.

I found this sample of code that may help but am not sure how to implement it in Excel 2012.
I need it to read all rows on the first sheet. and migrate them to the second sheet.

Sub SplitAll()
    Dim src As Range
    Dim result As Variant
    For Each src In Range("M:M").SpecialCells(xlCellTypeConstants)
        result = Split(src, "<")
        'last cell in column M
        With Cells(Rows.Count, 3).End(xlUp)
            Range(.Offset(1, 0), .Offset(1 + UBound(result, 1), 0)) = Application.WorksheetFunction.Transpose(result)
        End With
    Next src
End Sub

Open in new window

ExcelTransposeUsingDelimiter.xlsx
0
 
LVL 4

Expert Comment

by:andrew_man
Comment Utility
HI

okay

Andrew
example.xlsb
0
 
LVL 4

Expert Comment

by:andrew_man
Comment Utility
Hi all,

Okay!

amended!

Sub SplitAll()
    Dim src As Range
    Dim result As Variant
    For Each src In Range("M:M").SpecialCells(xlCellTypeConstants)
        result = Split(Application.WorksheetFunction.Substitute(src, "<", "[<"), "[")
        'last cell in column M
       With Cells(Rows.Count, 3).End(xlUp)
           Range(.Offset(1, 0), .Offset(1 + UBound(result, 1), 0)) = Application.WorksheetFunction.Transpose(result)
        End With
    Next src
End Sub


Andrew Man from Hong Kong
example1.xlsb
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 4

Accepted Solution

by:
andrew_man earned 500 total points
Comment Utility
The result moves to sheet2 now.....
example1.xlsb
0
 
LVL 7

Author Comment

by:XGIS
Comment Utility
Hello Andrew_man.... it is looking pretty good... funny comments..making excel interesting
I made some macro mods to try and eliminate the blank line space between the transposed records.
Is there a value I should change or add to remove the blank line spacing between the rows
Also a way to eliminate TRIM white-space  to the left or right of each value new value. It only seems to be affecting the last row of the first transpose row </STMTTRN>
0
 
LVL 7

Author Closing Comment

by:XGIS
Comment Utility
Thankyou all for your input.. Special thanks to Andrew_Man for solving this issue.
I ended up just using F5 - Special - Blanks to remove lines and Trim and Clean to tidy the output.... Cheers Aaron
0
 
LVL 4

Expert Comment

by:andrew_man
Comment Utility
Sub SplitAll()
    Dim src As Range
    Dim result As Variant
    For Each src In Range("M:M").SpecialCells(xlCellTypeConstants)

        result = Split(Application.WorksheetFunction.Substitute(src, "<", "[<"), "[")
        'last cell in column M
       
       For i = 1 To UBound(result)
       result(i - 1) = result(i)
       Next i
       ReDim Preserve result(UBound(result) - 1)
   
       Sheets("Sheet2").Select
       
       With Cells(Rows.Count, 1).End(xlUp).Offset(0, 0)
       
       
           Range(.Offset(1, 0), .Offset(1 + UBound(result, 1), 0)) = Application.WorksheetFunction.Transpose(result)
       End With
       
             
       Sheets("Sheet1").Select
       
    Next src
End Sub
0
 
LVL 7

Author Comment

by:XGIS
Comment Utility
Hello Andrew, Nothing short of spectacular...Thankyou for the extra input.

Awesome
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

762 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

9 Experts available now in Live!

Get 1:1 Help Now