Rearranging a table layout into a custom format

ajaysomasi

New Member
Joined
Mar 6, 2017
Messages
25
Hi all

Appreciate if you can provide me with a VBA to undertake the below task..

I want the program to take 1 under each column and report the applicable rows for that column in the format shown after the table.. Appreciate your help..

Subject ASubject BSubject C
Row11N1
Row2NN1
Row31NN
Row4N1N
Row511N

The resulting format should be:

Subject A
Row1
Row3
Row5

Subject B
Row4
Row5

Subject C
Row1
Row2
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Book12
AB
1Column1Attribute
2Row3Subject A
3Row5Subject A
4Row1Subject A
5Row5Subject B
6Row4Subject B
7Row2Subject C
8Row1Subject C
Sheet6


With Power Query, Unpivot your data and sort your columns

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Subject A", type any}, {"Subject B", type any}, {"Subject C", type any}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
    #"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] = 1)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Attribute", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Value"})
in
    #"Removed Columns"
 
Upvote 0
Try this. Your data on Sheet1 starting in cell A1, results on Sheet2
Dante Amor
ABCD
1Subject ASubject BSubject C
2Row11N1
3Row2NN1
4Row31NN
5Row4N1N
6Row511N
Sheet1

Dante Amor
A
1
2Subject A
3Row1
4Row3
5Row5
6
7Subject B
8Row4
9Row5
10
11Subject C
12Row1
13Row2
14
Sheet2


VBA Code:
Sub Rearranging_table()
  Dim i As Long, j As Long, k As Long, lr As Long, lc As Long, m As Long
  Dim a As Variant
  
  With Sheets("Sheet1")
    lr = .Range("A" & Rows.Count).End(3).Row
    lc = .Cells(1, Columns.Count).End(1).Column
    a = .Range("A1", .Cells(lr, lc)).Value2
  End With
  ReDim b(1 To (UBound(a, 1) * UBound(a, 2)) + (UBound(a, 2) * 2), 1 To 1)
  
  k = 1
  For j = 2 To UBound(a, 2)
    m = k
    For i = 2 To UBound(a, 1)
      If a(i, j) = 1 Then
        k = k + 1
        b(m, 1) = a(1, j)
        b(k, 1) = a(i, 1)
      End If
    Next i
    If m <> k Then k = k + 2
  Next j
  Sheets("Sheet2").Range("A:A").ClearContents
  Sheets("Sheet2").Range("A2").Resize(k, 1).Value = b
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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