+ Reply to Thread
Results 1 to 18 of 18

Data Layout Conversion (Advanced Rows to Column ?)

Hybrid View

bibekpd Data Layout Conversion... 10-10-2015, 01:05 PM
TMS Re: Data Layout Conversion... 10-10-2015, 01:35 PM
bibekpd Re: Data Layout Conversion... 10-10-2015, 01:57 PM
TMS Re: Data Layout Conversion... 10-10-2015, 02:10 PM
bibekpd Re: Data Layout Conversion... 10-10-2015, 02:38 PM
karedog Re: Data Layout Conversion... 10-10-2015, 02:33 PM
bibekpd Re: Data Layout Conversion... 10-10-2015, 02:44 PM
karedog Re: Data Layout Conversion... 10-10-2015, 02:53 PM
bibekpd Re: Data Layout Conversion... 10-10-2015, 03:09 PM
karedog Re: Data Layout Conversion... 10-10-2015, 03:13 PM
bibekpd Re: Data Layout Conversion... 10-10-2015, 03:24 PM
karedog Re: Data Layout Conversion... 10-10-2015, 03:30 PM
jindon Re: Data Layout Conversion... 10-10-2015, 04:14 PM
TMS Re: Data Layout Conversion... 10-10-2015, 08:14 PM
bibekpd Re: Data Layout Conversion... 10-11-2015, 06:22 AM
TMS Re: Data Layout Conversion... 10-11-2015, 06:40 AM
bibekpd Re: Data Layout Conversion... 10-11-2015, 07:18 AM
TMS Re: Data Layout Conversion... 10-11-2015, 09:24 AM
  1. #1
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Question Data Layout Conversion (Advanced Rows to Column ?)

    Hi,

    I have a large CSV (comma delimited) which has data laid out in rows and each row contains about 10,000 separate inputs. This data is an output from a Simulink Simulation where variables have been saved on to the workspace saved as a .mat file then converted into csv (using a tool my colleague wrote). This tool was a modification from a script provided by mathworks therefore quite complex to modify to save the data in the format I need.

    Ive attached a spreadsheet with this post which contains the format the data currently is (input) and the format I need it to be in (output). As its an output from simulink some data are multidimensional arrays therefore not all the data can be converted straight from rows to columns. I have used the "Paste Special" and transposed the data for now, but I will need to do this repeatedly therefore looking for a better solution i.e. a vbscript that can do this for me.

    Any advice/solution would be appreciated.

    Thanks.

    Edit: Forgot to mention, the data might always not be in the same format. For e.g. sometimes the data might just be in the following format without any multidimensional arrays..
    Header 1
    1,2,3,4,5...
    Header 2
    3,4,5,6,7..
    Header 3.. and so on.
    Attached Files Attached Files
    Last edited by bibekpd; 10-10-2015 at 01:37 PM. Reason: Further Info.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,143

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Where you have Header 1, Header 2, etc., what do these headers really look like?
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    These header can be anything as they are whatever I set the Simulink workspace variables as.

    Currently they are I_ResolverValidity or I_<xx> for Inputs and O_MonitorFailed or O_<xx> for Outputs. If it helps they can be fully set as a string...

    I should have also mentioned the input are all integers. Boolean are represented as 1 & 0s.

    I was thinking of character comparison for each row and depending on the character type, decide which row to begin the column from ? That way I could decide if its a header (first row & x column) , array count [0] , [1] ... etc (second row & x column) and finally integer 1,-2,3.. etc to begin from third row and x column. Hope this makes sense.
    Last edited by bibekpd; 10-10-2015 at 02:01 PM. Reason: typo

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,143

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    It makes sense, but I'd like to see some more realistic examples.

    I'd like to help you with this but, if I work with what you have provided (even with an explanation), and it doesn't work with your real life data, you're on your own.

    I don't want to go through the process twice.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Post Re: Data Layout Conversion (Advanced Rows to Column ?)

    Quote Originally Posted by TMS View Post
    It makes sense, but I'd like to see some more realistic examples.

    I'd like to help you with this but, if I work with what you have provided (even with an explanation), and it doesn't work with your real life data, you're on your own.

    I don't want to go through the process twice.

    Regards, TMS
    TMS, I completely understand your concern but unfortunately I cannot provide the exact data due to security restrictions at my work however I have created a sample csv with almost identical data but only for first 30 inputs.

    I hope I will be able to modify what you've started to suit my needs if it doesn't work with the real data on Monday.

    Please find attached.

    Thanks
    Attached Files Attached Files

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Maybe :

    Sub Test()
      Dim ws As Worksheet, cell As Range
      Application.ScreenUpdating = False
      ActiveSheet.Copy after:=Sheets(Sheets.Count): Set ws = ActiveSheet
      Columns("A:B").Insert xlShiftToRight
      For Each cell In Intersect(ActiveSheet.UsedRange, Columns("C"))
          If Len(cell) And Not Len(cell.Offset(, 1)) Then
             If Left(cell.Value, 1) = "[" Then
                cell.Cut cell.Offset(1, -1)
             Else
                If Left(cell.Offset(1).Value, 1) = "[" Then cell.Cut cell.Offset(2, -2) Else cell.Cut cell.Offset(1, -2)
             End If
          End If
      Next cell
      Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
      Range("A1").CurrentRegion.Copy: Sheets.Add after:=Sheets(Sheets.Count)
      Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
      With Application
        .DisplayAlerts = False: ws.Delete: .DisplayAlerts = True
        .ScreenUpdating = True
      End With
    End Sub
    Last edited by karedog; 10-10-2015 at 02:58 PM. Reason: code correction

  7. #7
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Quote Originally Posted by karedog View Post
    Maybe :

    Sub Test()
      Dim ws As Worksheet, cell As Range
      Application.ScreenUpdating = False
      ActiveSheet.Copy after:=Sheets(Sheets.Count): Set ws = ActiveSheet
      Columns("A:B").Insert xlShiftToRight
      For Each cell In Intersect(ActiveSheet.UsedRange, Columns("C"))
          If Len(cell) And Not Len(cell.Offset(, 1)) Then
             If Left(cell.Value, 1) = "[" Then
                cell.Cut cell.Offset(1, -1)
             Else
                If Left(cell.Offset(1).Value, 1) = "[" Then cell.Cut cell.Offset(2, -2) Else cell.Cut cell.Offset(1, -2)
             End If
          End If
      Next cell
      Intersect(ActiveSheet.UsedRange, Columns("C")).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
      Range("A1").CurrentRegion.Copy: Sheets.Add after:=Sheets(Sheets.Count)
      Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
      With Application
        .DisplayAlerts = False: ws.Delete: .DisplayAlerts = True
        .ScreenUpdating = True
      End With
    End Sub
    karedog, thank you. it works with my first attachment but runtime error with the sample csv with 30 inputs which subsequently needs to be increased to 10000 for the actual set of data. This will also have 10+ headers and an "undefined" layout per se, in which the csv might contain multidimensional arrays or not and might not always be in the exact row every time.

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    How many rows do you have ? If it is exceeding 16384, then surely it will cause error, since the transposed data exceeded Excel's maximum columns (16384)

  9. #9
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    No, rows will not exceed 100 even if i export all variables in the same file.

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    I have modified my code (correction to one line of code), please check it out. I have test with your sample files at post #1 and #6, and to me, it seem to work very well.

  11. #11
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Quote Originally Posted by karedog View Post
    I have modified my code (correction to one line of code), please check it out. I have test with your sample files at post #1 and #6, and to me, it seem to work very well.
    Works with my sample csv as well now. Will use it on the actual data and report back. Quite confident, it'll work.

    Cheeers!
    Last edited by bibekpd; 10-10-2015 at 03:24 PM. Reason: typo

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    I hope so, and thank you for the reputation points.


    Regards

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Alternative.

    This will read the data directly from cvs file.
    Sub test()
        Dim fn As String, txt As String, w, x, y
        Dim i As Long, ii As Long, ub As Long, flg As Boolean
        fn = Application.GetOpenFilename("CSVFiles,*.csv")
        If fn = "False" Then Exit Sub
        txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).REadAll
        x = Split(txt, vbCr): ub = UBound(Split(x(0), ","))
        With CreateObject("VBScript.RegExp")
            For i = 0 To UBound(x)
                y = Split(x(i), ",")
                .Pattern = "^([^,\[]+),+$"
                If .test(x(i)) Then
                    If IsEmpty(w) Then
                        ReDim w(1 To ub + 100, 1 To 1)
                    Else
                        ReDim Preserve w(1 To UBound(w, 1), 1 To UBound(w, 2) + 1)
                    End If
                    w(1, UBound(w, 2)) = .Execute(x(i))(0).submatches(0): flg = False
                End If
                .Pattern = "^(\[\d+\]).*"
                If .test(x(i)) Then
                    If flg Then ReDim Preserve w(1 To UBound(w, 1), 1 To UBound(w, 2) + 1)
                    w(2, UBound(w, 2)) = .Execute(x(i))(0).submatches(0): flg = True
                End If
                .Pattern = "^(-?\d+(\.\d+)?,?)+.*"
                If .test(x(i)) Then
                    For ii = 0 To UBound(y)
                        w(ii + 3, UBound(w, 2)) = y(ii)
                    Next
                End If
            Next
        End With
        Sheets("sheet1").Cells(1).Resize(UBound(w, 1), UBound(w, 2)).Value = w
    End Sub
    Last edited by jindon; 10-10-2015 at 04:32 PM.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,143

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    I don't know ... you turn your back for 5 minutes

    Looks like you are in capable hands so I will bow out gracefully.


    Regards, TMS

  15. #15
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Quote Originally Posted by TMS View Post
    I don't know ... you turn your back for 5 minutes

    Looks like you are in capable hands so I will bow out gracefully.


    Regards, TMS
    You can help me understand karedog's solution or maybe point me in the right direction :P I wish to understand the macro before using it.. I only have a very vague idea of whats going on from "Columns("A:B").Insert xlShiftToRight" line :/

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,143

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Always run (new, untested) macros on a copy of your data, not the live file ... or make a backup copy of the live file first.

    Option Explicit
    
    Sub Test()
      Dim ws As Worksheet, cell As Range
      Application.ScreenUpdating = False
      ActiveSheet.Copy after:=Sheets(Sheets.Count): Set ws = ActiveSheet
      ' move all the data to the right
      Columns("A:B").Insert xlShiftToRight
      ' check each cell in column C with data
      For Each cell In Intersect(ActiveSheet.UsedRange, Columns("C"))
          ' if the cell is NOT blank and the cell to the right IS blank
          If Len(cell) And Not Len(cell.Offset(, 1)) Then
             ' if the first character in the cell is "[" ...
             If Left(cell.Value, 1) = "[" Then
                ' cut the cell and move it down 1 row, back 1 column
                cell.Cut cell.Offset(1, -1)
             Else
                ' otherwise, if the next cell down first character is "["
                If Left(cell.Offset(1).Value, 1) = "[" Then
                    ' cut the cell and put it 2 cells down and 2 columns back
                    cell.Cut cell.Offset(2, -2)
                Else
                    ' or cut the cell and put it 1 cells down and 2 columns back
                    cell.Cut cell.Offset(1, -2)
                End If
             End If
          End If
      Next cell
      ' delete the rows that now have blanks in column C
      Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
      ' copy the data; create a new worksheet ... which will become the active sheet
      Range("A1").CurrentRegion.Copy: Sheets.Add after:=Sheets(Sheets.Count)
      ' transpose the restructured data into the new worksheet
      Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
      With Application
        ' delete the original data
        ' I'd probably not do this so that the macro can be re-run
        .DisplayAlerts = False: ws.Delete: .DisplayAlerts = True
        .ScreenUpdating = True
      End With
    End Sub

    Regards, TMS

  17. #17
    Registered User
    Join Date
    11-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    Makes much more sense now. Thank you TMS.

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,143

    Re: Data Layout Conversion (Advanced Rows to Column ?)

    You're welcome. Thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Conversion of Set of Rows ro Column in new sheet.
    By apowale in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2013, 07:40 AM
  2. Data layout between row and column
    By scottb in forum Excel General
    Replies: 3
    Last Post: 12-07-2010, 12:01 AM
  3. Convert data from column layout to row layout
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2010, 03:28 PM
  4. layout the column from data
    By eb316 in forum Excel General
    Replies: 9
    Last Post: 09-17-2009, 03:03 PM
  5. Auto Layout Of Data In Column
    By ethanjames in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 05-28-2009, 09:32 PM
  6. Advanced Filter to return Rows w/blank column
    By ChemistB in forum Excel General
    Replies: 3
    Last Post: 04-01-2009, 01:14 PM
  7. data conversion to rows
    By anandapani in forum Excel General
    Replies: 1
    Last Post: 12-15-2008, 04:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1