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.
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.
Done..
Formula:
=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)))))
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.
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)) )
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))),"")))))))
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
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),"")),"")))))))
That worked.
Thanks windknife!
You are welcome.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks