+ Reply to Thread
Results 1 to 9 of 9

Rearranging Data Based on Delimiter

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Rearranging Data Based on Delimiter

    Hi,

    I have a dataset with data in rows, each row has multiple items separated by CHAR(10).
    Im trying to get a dynamic solution to rearrange this dataset to show each line separately for each item in the row.
    Attached sheet with desired results.

  2. #2
    Forum Expert
    Join Date
    10-19-2021
    Location
    Brazil
    MS-Off Ver
    Office 365 V2401 w/ Win10 Home 64 Bit
    Posts
    2,014

    Re: Rearranging Data Based on Delimiter

    Done..

    Formula: copy to clipboard
    =LET(a,TRANSPOSE(SPLITTEXT(TEXTJOIN(";",1,""&SUBSTITUTE(C3:F4,CHAR(10),"")),"",";",1,,"")),
    d,SORT(SPLITTEXT(REPT(CONCAT(B3:B4&";"),2),,";",1)),VSTACK(B2:F2,HSTACK(--d,VSTACK(TAKE(a,,4),DROP(a,,4)))))

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Rearranging Data Based on Delimiter

    It took some VB code to make it happen. The code is commented as to where you have to make changes to fit the final product.

    Basically, the code uses the SPLIT command in VB to sort the parts into an array and then prints one row per array part.

    Sub ExpandIt()
    Dim i As Long           ' Row index
    Dim j As Long           ' Column index
    Dim k As Long           ' Array Index
    Dim NewDate As Date     ' Date
    Dim MyStr() As String   ' Array with input
    Dim RowNum As Long      ' Working Row Number
    Dim LRowNum As Long     ' Last row for date
    Dim sht1 As Worksheet   ' Sheet with the raw data
    Dim sht2 As Worksheet   ' Sheet with output
    
    ' Change these if you have to
    Const StartRow = 3
    Const EndRow = 4
    Const StartCol = 2
    Const EndCol = 6
    
    ' Initalize variables
    Set sht1 = Sheets("Sheet1")     ' Change as required
    Set sht2 = Sheets("Sheet2")     ' Change as required
    
    ' Clear the output sheet
    sht2.Cells.ClearContents
    
    ' Copy the header row
    sht1.Range(sht1.Cells(StartRow - 1, StartCol), sht1.Cells(StartRow - 1, EndCol)).Copy sht2.Cells(StartRow - 1, StartCol)
    
    ' Loop through the rows
    For i = StartRow To EndRow
        NewDate = sht1.Cells(i, StartCol)
        ' Set the last row - start at startrow then calculate after last date
        If i = StartRow Then
            LRowNum = StartRow
        Else
            LRowNum = sht2.Cells(Rows.Count, StartCol).End(xlUp).Row + 1
        End If
        
        ' Loop through columns
        For j = StartCol + 1 To EndCol
            ' Set the Rownum
            RowNum = LRowNum
            MyStr = Split(sht1.Cells(i, j), Chr(10))
            For k = 0 To UBound(MyStr)
                sht2.Cells(RowNum + k, StartCol) = NewDate
                sht2.Cells(RowNum + k, j) = MyStr(k)
            Next k
        Next j
    Next i
    
    End Sub
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Forum Expert
    Join Date
    04-14-2009
    Location
    Taiwan
    MS-Off Ver
    Excel 2016,2019,O365
    Posts
    2,917

    Re: Rearranging Data Based on Delimiter

    Another way,

    =LET(
      a,C3:F4,
      b,C2:F2,
      c,B3:B4,
      d,IFERROR(REDUCE(HSTACK("",b),SEQUENCE(ROWS(a)),LAMBDA(x,y,VSTACK(x,REDUCE(INDEX(c,y,1),SEQUENCE(COLUMNS(b)),LAMBDA(m,n,HSTACK(m,TEXTSPLIT(INDEX(a,y,n),,CHAR(10),1))))))),""),
      e,SCAN(,INDEX(d,,1),LAMBDA(x,y,IF(y="",x,y))),
      HSTACK(e,DROP(d,,1))
    )

  5. #5
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: Rearranging Data Based on Delimiter

    Elaborating on Windknife's formula, here is a shorter formula:

    Please try:
    =LET(a,C3:F4,b,C2:F2,c,B3:B4,
      REDUCE(HSTACK("",b),SEQUENCE(ROWS(a)),LAMBDA(x,y,VSTACK(x,REDUCE(INDEX(c,y,1),INDEX(a,y,),LAMBDA(m,n,IFNA(HSTACK(m,TEXTSPLIT(n,,CHAR(10))),"")))))))

  6. #6
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Rearranging Data Based on Delimiter

    Hi All

    Thanks for the solutions
    In my dataset there are some empty cells, so the formula is failing
    Could you amend the formula to consider empty cells in between

  7. #7
    Forum Expert
    Join Date
    04-14-2009
    Location
    Taiwan
    MS-Off Ver
    Excel 2016,2019,O365
    Posts
    2,917

    Re: Rearranging Data Based on Delimiter

    Try this,

    =LET(a,C3:F4,b,C2:F2,c,B3:B4,
      REDUCE(HSTACK("",b),SEQUENCE(ROWS(a)),LAMBDA(x,y,VSTACK(x,REDUCE(INDEX(c,y,1),INDEX(a,y,),LAMBDA(m,n,IFERROR(HSTACK(m,IFERROR(TEXTSPLIT(n,,CHAR(10),0),"")),"")))))))

  8. #8
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Rearranging Data Based on Delimiter

    That worked.

    Thanks windknife!

  9. #9
    Forum Expert
    Join Date
    04-14-2009
    Location
    Taiwan
    MS-Off Ver
    Excel 2016,2019,O365
    Posts
    2,917

    Re: Rearranging Data Based on Delimiter

    You are welcome.

+ 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. Replies: 10
    Last Post: 09-17-2021, 05:36 PM
  2. Text to columns based on just first delimiter
    By Afsheen in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-19-2019, 12:07 AM
  3. Rearranging text based on spaces and comma
    By xo1darcie1ox in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-30-2017, 02:26 PM
  4. Rearranging the contents of the worksheet based on the cell Value
    By chunkyp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2017, 02:54 PM
  5. [SOLVED] Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post back
    By capson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-05-2016, 03:54 PM
  6. [SOLVED] Splitting data based on delimiter
    By sathishkm in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-11-2014, 05:59 PM
  7. [SOLVED] Rearranging the data set based on multiple rules.
    By sulin360 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-17-2013, 10:33 AM

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