Exchange rows and columns

RichardL69

New Member
Joined
Feb 22, 2021
Messages
3
Office Version
  1. 365
Hi, Hope everyone is well?
I receive a file with a box number at the top, and a list of items below similar to:
123
AAAEEEIII
BBBFFFJJJ
CCCGGGKKK
DDDHHHLLL
What I need to do is change the listing to be a box number for each part supplied, like the below:
1AAA
1BBB
1CCC
1DDD
2EEE
2FFF
2GGG
2HHH
3III
3JJJ
3KKK
3LLL
Does anyone know of a formula where i can list easily what is required without dragging and dropping?
I can switch with copy and paste and put everything left to right, but i need to assign a box number to every part?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You can use copy/paste special and check the transpose button in the lower right of the "Paste Special" box. Do you need a formula for this project?
 
Upvote 0
You can use copy/paste special and check the transpose button in the lower right of the "Paste Special" box. Do you need a formula for this project?
Hi, yes i can transpose the values but i can't get a formula to look up each box number and list as per the last picture?
 
Upvote 0
Hi @RichardL69. Welcome to the board!

Glad to hear you got the solution.

Do you mind posting about your solution (the macro you wrote) to help future readers? Then it is perfectly fine to mark your post as the solution.
 
Upvote 0
An alternative solution is to use Power Query called Get and Transform Data in 365 and found on the Data Tab
Unpivot the data on the Tranform Tab of the PQ Editor.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Columns",{{"Attribute", Order.Ascending}})
in
    #"Sorted Rows"

Book7
AB
1AttributeValue
21BBB
31DDD
41CCC
51AAA
62GGG
72FFF
82HHH
92EEE
103LLL
113III
123JJJ
133KKK
Table1
 
Upvote 0
Solution
Quick and clean solution with Power Query, @alansidman. I would mark it as the solution if @RichardL69 doesn't prefer to share the mentioned solution with us.

I thought it might be a good opportunity to make practice with LET function and creating a loop-kind behavior as I really like LET and LAMBDA recently (Office 365):
1162593.xlsm
ABCDEF
1123A1:C5
2AAAEEEIII1AAA
3BBBFFFJJJ1BBB
4CCCGGGKKK1CCC
5DDDHHHLLL1DDD
62EEE
72FFF
82GGG
92HHH
103III
113JJJ
123KKK
133LLL
LET_Function
Cell Formulas
RangeFormula
E2:F13E2= LET(rng, A1:C5, cols, COLUMNS(rng), rows, ROWS(rng)-1, data, OFFSET(rng,1,,rows,cols), seq, SEQUENCE(rows*cols,2), IF(ISEVEN(seq), LET( x, MOD(seq/2-1,rows)+1, y, ROUNDDOWN((seq/2-1)/rows,0)+1, INDEX(data,x,y) ), INDEX(rng,1,ROUNDDOWN(((seq+1)/2-1)/rows,0)+1) ) )
Dynamic array formulas.


Similar solution by using a user-defined function:
VBA Code:
Function DISTRIBUTE(rng As Range)
Dim col As Range
Dim cll As Range
Dim i As Integer

    ReDim arr((rng.Rows.Count - 1) * rng.Columns.Count - 1, 1)
  
    For Each col In rng.Columns
        For Each cll In rng.Offset(1, col.Column - rng.Column).Resize(rng.Rows.Count - 1, 1).Cells
            arr(i, 0) = col.Cells(1).Value
            arr(i, 1) = cll.Value
            i = i + 1
        Next cll
    Next col
    DISTRIBUTE = arr
End Function

Result:
1162593.xlsm
ABCDEF
1123A1:C5
2AAAEEEIII1AAA
3BBBFFFJJJ1BBB
4CCCGGGKKK1CCC
5DDDHHHLLL1DDD
62EEE
72FFF
82GGG
92HHH
103III
113JJJ
123KKK
133LLL
UDF
Cell Formulas
RangeFormula
E2:F13E2=DISTRIBUTE(A1:C5)
Dynamic array formulas.


And while we already have the UDF, a function-less solution with VBA - takes the selection as the data range:
VBA Code:
Sub doDistrubute()
Dim rng As Range
Dim col As Range
Dim cll As Range
Dim i As Integer

    Set rng = Selection
    ReDim arr((rng.Rows.Count - 1) * rng.Columns.Count - 1, 1)
  
    For Each col In rng.Columns
        For Each cll In rng.Offset(1, col.Column - rng.Column).Resize(rng.Rows.Count - 1, 1).Cells
            arr(i, 0) = col.Cells(1).Value
            arr(i, 1) = cll.Value
            i = i + 1
        Next cll
    Next col
    rng.Offset(, rng.Columns.Count + 1).Resize(UBound(arr, 1) + 1, UBound(arr, 2) + 1).Value = arr
End Sub

Obviously, all versions work with the provided range, not limited to the sample range in the original question.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top