Transposing Info From Columns to Rows - Irregular Pattern

SimonHughes

Well-known Member
Joined
Sep 16, 2009
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a data set which is in column format and I need to transpose this to row format. There are four columns which could but will not always, have nil, 1, 2 3 or 4 entries and these need to be transposed in rows. The snip below shows original table and the desired outcome. Any help appreciated, many thanks.

Whitbread Remedials Temp.xlsx
BCDEFG
2Ref#LocationComment 1Comment 2Comment 3Comment 4
31ADRepair linkRemove port
42FD
53SWRepair linkClean duct
64ABClean ductReplace hatch
7
8
9Ref#LocationAction
101ADRepair link
111ADRemove port
123SWRepair link
133SWClean duct
144ABClean duct
155ABReplace hatch
Sheet2
 

Attachments

  • 1723187076331.png
    1723187076331.png
    12.9 KB · Views: 2
  • 1723187118427.png
    1723187118427.png
    12.9 KB · Views: 2

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Make the source data into a table, load it into power query, then select the first two columns, right-click a header and choose Unpivot other columns. Job done. :)
 
Upvote 0
Make the source data into a table, load it into power query, then select the first two columns, right-click a header and choose Unpivot other columns. Job done. :)
Ah, yes, I will need to do a bit of research into Power Query - but it may be worth it, thanks.
 
Upvote 0
Here's a macro :
VBA Code:
Sub Transpose()

Dim Ws1 As Worksheet: Set Ws1 = Sheets("Sheet1") 'Change as required
Dim Ws2 As Worksheet: Set Ws2 = Sheets("Sheet2") 'Change as required
Dim P1 As Range: Set P1 = Ws1.UsedRange
Dim T1: T1 = P1
Dim T2()
Dim a%: a = 1
Dim i%, j%
ReDim Preserve T2(1 To 3, 1 To a)
T2(1, a) = "Ref#"
T2(2, a) = "Location"
T2(3, a) = "Action"
a = a + 1

For i = 2 To UBound(T1)
    For j = 3 To UBound(T1, 2)
        If T1(i, j) = "" Then GoTo n
        ReDim Preserve T2(1 To 3, 1 To a)
        T2(1, a) = T1(i, 1)
        T2(2, a) = T1(i, 2)
        T2(3, a) = T1(i, j)
        a = a + 1
n:  Next j
Next i

Ws2.[A1].Resize(UBound(T2, 2), UBound(T2)) = Application.Transpose(T2)
End Sub
 
Upvote 0
.. or a formula? (I'm assuming B15 in your mini sheet is an error?)
I have offered two options. The formula in B10 returns the Ref# numbers as Text. If they need to be returned as Numbers then the B18 formula will do that.

24 08 09.xlsm
BCDEFG
2Ref#LocationComment 1Comment 2Comment 3Comment 4
31ADRepair linkRemove port
42FD
53SWRepair linkClean duct
64ABClean ductReplace hatch
7
8
9Ref#LocationAction
101ADRepair link
111ADRemove port
123SWRepair link
133SWClean duct
144ABClean duct
154ABReplace hatch
16
17
181ADRepair link
191ADRemove port
203SWRepair link
213SWClean duct
224ABClean duct
234ABReplace hatch
Cols to Rows
Cell Formulas
RangeFormula
B10:D15B10=TEXTSPLIT(TEXTJOIN("#",1,IF(D3:G6="","",B3:B6&"|"&C3:C6&"|"&D3:G6)),"|","#")
B18:D23B18=LET(t,TEXTSPLIT(TEXTJOIN("#",1,IF(D3:G6="","",B3:B6&"|"&C3:C6&"|"&D3:G6)),"|","#"),IFERROR(--t,t))
Dynamic array formulas.
 
Upvote 0
Two clicks in power query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    unPiv = Table.UnpivotOtherColumns(Source, {"Ref#", "Location"}, "Attribute", "Action"),
    delCol = Table.RemoveColumns(unPiv,{"Attribute"})
in
    delCol
 
Upvote 0
Or a formula

Excel Formula:
=LET(T,C3:F6,HSTACK(TOCOL(IFS(T<>"",A3:A6),2),TOCOL(IFS(T<>"",B3:B6),2),TOCOL(T,1)))
 
Upvote 0
.. or a formula? (I'm assuming B15 in your mini sheet is an error?)
I have offered two options. The formula in B10 returns the Ref# numbers as Text. If they need to be returned as Numbers then the B18 formula will do that.

24 08 09.xlsm
BCDEFG
2Ref#LocationComment 1Comment 2Comment 3Comment 4
31ADRepair linkRemove port
42FD
53SWRepair linkClean duct
64ABClean ductReplace hatch
7
8
9Ref#LocationAction
101ADRepair link
111ADRemove port
123SWRepair link
133SWClean duct
144ABClean duct
154ABReplace hatch
16
17
181ADRepair link
191ADRemove port
203SWRepair link
213SWClean duct
224ABClean duct
234ABReplace hatch
Cols to Rows
Cell Formulas
RangeFormula
B10:D15B10=TEXTSPLIT(TEXTJOIN("#",1,IF(D3:G6="","",B3:B6&"|"&C3:C6&"|"&D3:G6)),"|","#")
B18:D23B18=LET(t,TEXTSPLIT(TEXTJOIN("#",1,IF(D3:G6="","",B3:B6&"|"&C3:C6&"|"&D3:G6)),"|","#"),IFERROR(--t,t))
Dynamic array formulas.
Hi Peter, this formula works well but unfortunately, I have over simplified the example. I have the data on one worksheet and the returns on another. Also, the columns that I want to get returns from are not contiguous. Can I upload a worksheet somehow?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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