Rearrange Table and Column

LambVBA

New Member
Joined
Mar 28, 2014
Messages
16
I'm having difficulty reorganizing a set of tables and their columns within. I currently have close to 14K rows of data. As simple as it sounds, I cannot seem to find a fix to go from its vertical layout to a horizontal. Your help would be much appreciated. Below is an example of what my data currently looks like and the next example is what I hope to achieve. Needless to say I made the data rather generic in the examples.
table_namecolumn_name
STSSTS Column 1
STSSTS Column 2
STSSTS Column 3
STSSTS Column 4
STSSTS Column 5
STSSTS Column 6
ADJADJ Column 1
ADJADJ Column 2
ADJADJ Column 3
ADJADJ Column 4
ADJADJ Column 5
ADRADR Column 1
ADRADR Column 2
ADRADR Column 3
ADRADR Column 4
ADRADR Column 5
ADRADR Column 6
ADRADR Column 7

<tbody>
</tbody>

Hopeful Outcome
STSADJADR
STS Column 1ADJ Column 1ADR Column 1
STS Column 2ADJ Column 2ADR Column 2
STS Column 3ADJ Column 3ADR Column 3
STS Column 4ADJ Column 4ADR Column 4
STS Column 5ADJ Column 5ADR Column 5
STS Column 6ADR Column 6
ADR Column 7

<tbody>
</tbody>
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
See if this is what you want.
Rich (BB code):
Sub Rearrange()
  Dim aColVals
  Dim i As Long
  
  Const sColVals As String = "STS,ADJ,ADR"
  
  aColVals = Split(sColVals, ",")
  Application.ScreenUpdating = False
  With Range("A1", Range("B" & Rows.Count).End(xlUp))
    For i = 0 To UBound(aColVals)
      .AutoFilter Field:=1, Criteria1:=aColVals(i)
      .Columns(2).Copy Destination:=Range("E1").Offset(, i)
    Next i
    .Parent.AutoFilterMode = False
  End With
  Range("E1").Resize(, 3).Value = aColVals
  Application.ScreenUpdating = True
End Sub


Starting with columns A:B below, the code has produced columns E:G

Excel Workbook
ABCDEFG
1table_namecolumn_nameSTSADJADR
2STSSTS Column 1STS Column 1ADJ Column 1ADR Column 1
3STSSTS Column 2STS Column 2ADJ Column 2ADR Column 2
4STSSTS Column 3STS Column 3ADJ Column 3ADR Column 3
5STSSTS Column 4STS Column 4ADJ Column 4ADR Column 4
6STSSTS Column 5STS Column 5ADJ Column 5ADR Column 5
7STSSTS Column 6STS Column 6ADR Column 6
8ADJADJ Column 1ADR Column 7
9ADJADJ Column 2
10ADJADJ Column 3
11ADJADJ Column 4
12ADJADJ Column 5
13ADRADR Column 1
14ADRADR Column 2
15ADRADR Column 3
16ADRADR Column 4
17ADRADR Column 5
18ADRADR Column 6
19ADRADR Column 7
20
Rearrange
 
Upvote 0
This code does work, but is there a way to pickup from column A what the table name should be? There are over 900 distinct table headers, not just the 3 (STS, ADJ, ADR).
 
Upvote 0
This code does work, but is there a way to pickup from column A what the table name should be? There are over 900 distinct table headers, not just the 3 (STS, ADJ, ADR).
That wasn't clear to start with but try this version.
Rich (BB code):
Sub RearrangeV2()
  Dim aColVals
  Dim i As Long

  Application.ScreenUpdating = False
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("E1"), Unique:=True
    aColVals = Range("E2", Range("E" & Rows.Count).End(xlUp)).Value
    Columns("E").ClearContents
    With .Resize(, 2)
      For i = 1 To UBound(aColVals, 1)
        .AutoFilter Field:=1, Criteria1:=aColVals(i, 1)
        .Columns(2).Copy Destination:=Range("E1").Offset(, i - 1)
        Range("E1").Offset(, i - 1).Value = aColVals(i, 1)
      Next i
    End With
    .Parent.AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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