Transpose rows with variable data into a single column

sonikshah

New Member
Joined
Jul 30, 2013
Messages
2
Hi,

I want to break down every row into multiple row where a part of it would be carried forward from the parent and the last column would be the transpose of the remaining part of the parent row. Here's an example.

Table 1 (static column A, B. variable column: C, D, E, F...):
Column AColumn B
​Column C​Column D​Column E​Column F
abcdefABCDEF
lmnpqrLMNPQRXYZ

<tbody>
</tbody>

I would want it to look like:
Table 2:
Column A
Column BColumn CColumn DColumn EColumn F
abc
defABC
abcdefDEF
lmnpqrLMN
lmn
pqrPQR
lmnpqrXYZ

<tbody>
</tbody>

There are almost a 1000 rows in my original table with the variable part of the rows ranging from 1 to 10 columns. This means that every row's static part can have 1-10 copies with only the last column changing. I hope the problem makes sense. I have looked at other topics in the forum and have found similar topics (transposing rows into single column), however I didn't find one with variable data in the rows, and where they dont have a static part that gets copied. Please help!

Thanks,
Sonik
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to MrExcel.

Try (changing the reference to Sheet1 if necessary):

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim ShNew As Worksheet
    Dim x As Integer
    Dim r As Integer
    Dim c As Integer
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A1").CurrentRegion
    Set ShNew = Worksheets.Add
    x = 1
    For r = 1 To Rng.Rows.Count
        For c = 3 To Rng.Columns.Count
            If Rng.Cells(r, c).Value <> "" Then
                ShNew.Cells(x, 1).Value = Rng.Cells(r, 1).Value
                ShNew.Cells(x, 2).Value = Rng.Cells(r, 2).Value
                ShNew.Cells(x, 3).Value = Rng.Cells(r, c).Value
                x = x + 1
            End If
        Next c
    Next r
End Sub
 
Upvote 0
Another one (hated to waist it).

Code:
[color=darkblue]Sub[/color] Transposer()
    [color=darkblue]Dim[/color] v [color=darkblue]As[/color] [color=darkblue]Variant[/color], i [color=darkblue]As[/color] [color=darkblue]Long[/color], j [color=darkblue]As[/color] [color=darkblue]Long[/color], k [color=darkblue]As[/color] Long
    v = ActiveSheet.UsedRange.Value
    Worksheets.Add After:=Sheets(Sheets.Count)
    Application.ScreenUpdating = [color=darkblue]False[/color]
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] [color=darkblue]UBound[/color](v, 1)
        [color=darkblue]For[/color] j = 3 [color=darkblue]To[/color] [color=darkblue]UBound[/color](v, 2)
            [color=darkblue]If[/color] Len(v(i, j)) [color=darkblue]Then[/color]
                k = k + 1
                Range("A" & k).Value = v(i, 1)
                Range("B" & k).Value = v(i, 2)
                Range("C" & k).Value = v(i, j)
            [color=darkblue]Else[/color]
                [color=darkblue]Exit[/color] [color=darkblue]For[/color]
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Next[/color] j
    [color=darkblue]Next[/color] i
    Application.ScreenUpdating = [color=darkblue]False[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Here is anothe macro which, if you have lots of data, should be noticeably fast...

Code:
Sub RedistributeDataOnwardFromColumnC()
  Dim R As Long, C As Long, Index As Long, LastRow As Long, LastCol As Long
  Dim ArrIn As Variant, ArrOut As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = Rows("1:" & LastRow).Find(What:="*", SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, LookIn:=xlValues).Column
  ArrIn = Range("A1").Resize(LastRow, LastCol)
  ReDim ArrOut(1 To LastRow * (LastCol - 2), 1 To LastCol)
  Index = 1
  For R = 1 To LastRow
    For C = 3 To LastCol
      If Len(ArrIn(R, C)) Then
        ArrOut(Index, 1) = ArrIn(R, 1)
        ArrOut(Index, 2) = ArrIn(R, 2)
        ArrOut(Index, 3) = ArrIn(R, C)
        Index = Index + 1
      End If
    Next
  Next
  With Worksheets.Add
    .Range("A1").Resize(UBound(ArrOut), LastCol) = ArrOut
  End With
End Sub

Edit Note: Revised code posted at 2:12pm EDT
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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