Help with transpose

SeanDamnit

Board Regular
Joined
Mar 13, 2011
Messages
151
Hello collective consciousness of Mr. Excel,

I have an issue that I hope is relatively simple for someone out there.

I have a data set that looks a little like this:

a---1
a---2
a---3
b---4
c---5
c---6
d---7
d---8
d---9
d---10
.
.
.


and I'd like to make it look like this:

a---1---2---3
b---4
c---5---6
d---7---8---9---10
.
.
.


column 1 will always change...sometimes there will be 5 of the same, sometimes 1, sometimes 30. column 2 is a random number, not sequential if that matters.

I appreciate any guidance...thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi Sean,

Try the following code in a copy of your data:
(The original columns A and B will be deleted)

Code:
Sub Transpose()
Dim Lr As Integer, i As Integer, j As Integer, m As Integer

Application.ScreenUpdating = False
Lr = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To Lr
    If IsEmpty(Cells(m + 1, 3)) Then
        Count = WorksheetFunction.CountIf(Columns("A"), Cells(i, "A"))
        Cells(m + 1, 3) = Cells(i, "A")
    End If
        Cells(m + 1, j + 4) = Cells(i, "B")
        j = j + 1
    If j = Count Then m = m + 1: j = 0
Next i

Columns("A:B").Delete Shift:=xlToLeft
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Hope this helps.

Regards.
 
Upvote 0
Try...
Code:
Sub transpose()
last = Cells(Rows.Count, "C").End(xlUp).Row
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
    MsgBox last
    With WorksheetFunction.Application
        If Not IsError(.Index(Range("C2:C" & last), _
            .Match(Range("A" & i), Range("C2:C" & last), 0), 1)) = True Then
            Set D_Rng = Range("D" & .Match(Range("A" & i), Range("C2:C" & last), 0) + 1)
            D_Rng.Value = D_Rng & "--" & Range("B" & i)
        Else
            Range("C" & last + 1).Value = Range("A" & i)
            Range("D" & last + 1).Value = Range("B" & i)
            last = last + 1
        End If
    End With
Next
Columns("A:B").Delete Shift:=xlToLeft
End Sub

Try it with:
Col A_______Col B _____Col C ____Col D
input_______data ______Output____Data
a ___________ 1
a ___________ 2
b ___________ 3
c ___________ 4
c ___________ 5
d ___________ 6
 
Last edited:
Upvote 0
HI friend:)

How about this.
Excel Workbook
ABCDEFGHIJ
1Raw DataCountReturnValue
2a1a3123
3a2b14
4a3c256
5b4d478910
6c5
7c6
8d7
9d8
10d9
11d10
Sheet8
Excel 2010
Cell Formulas
RangeFormula
E2=COUNTIF($A$2:$A$11,D2)
F2=IF(COLUMNS($F2:F2)<=$E2,INDEX($B$2:$B$11,AGGREGATE(15,6,(ROW($A$2:$A$11)-ROW($A$2)+1)/($A$2:$A$11=$D2),COLUMNS($F2:F2))),"")


For E2 copy down
For F2 copy across and down

Hope it help too.
HOpe for feedback:)
 
Last edited:
Upvote 0
Hi Sean,

Try the following code in a copy of your data:
(The original columns A and B will be deleted)

Code:
Sub Transpose()
Dim Lr As Integer, i As Integer, j As Integer, m As Integer

Application.ScreenUpdating = False
Lr = Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To Lr
    If IsEmpty(Cells(m + 1, 3)) Then
        Count = WorksheetFunction.CountIf(Columns("A"), Cells(i, "A"))
        Cells(m + 1, 3) = Cells(i, "A")
    End If
        Cells(m + 1, j + 4) = Cells(i, "B")
        j = j + 1
    If j = Count Then m = m + 1: j = 0
Next i

Columns("A:B").Delete Shift:=xlToLeft
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Hope this helps.

Regards.

Works great, thanks!


Try...
Code:
Sub transpose()
last = Cells(Rows.Count, "C").End(xlUp).Row
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
    MsgBox last
    With WorksheetFunction.Application
        If Not IsError(.Index(Range("C2:C" & last), _
            .Match(Range("A" & i), Range("C2:C" & last), 0), 1)) = True Then
            Set D_Rng = Range("D" & .Match(Range("A" & i), Range("C2:C" & last), 0) + 1)
            D_Rng.Value = D_Rng & "--" & Range("B" & i)
        Else
            Range("C" & last + 1).Value = Range("A" & i)
            Range("D" & last + 1).Value = Range("B" & i)
            last = last + 1
        End If
    End With
Next
Columns("A:B").Delete Shift:=xlToLeft
End Sub

Try it with:
Col A_______Col B _____Col C ____Col D
input_______data ______Output____Data
a ___________ 1
a ___________ 2
b ___________ 3
c ___________ 4
c ___________ 5
d ___________ 6

This actually didn't work - a popup menu appeared simply saying "60", then everytime I hit "ok" a new popup appeared counting up to 100 something. The macro stopped after this.


HI friend:)

How about this.
Excel Workbook
ABCDEFGHIJ
1Raw DataCountReturnValue
2a1a3123
3a2b14
4a3c256
5b4d478910
6c5
7c6
8d7
9d8
10d9
11d10
Sheet8
Excel 2010
Cell Formulas
RangeFormula
E2=COUNTIF($A$2:$A$11,D2)
F2=IF(COLUMNS($F2:F2)<=$E2,INDEX($B$2:$B$11,AGGREGATE(15,6,(ROW($A$2:$A$11)-ROW($A$2)+1)/($A$2:$A$11=$D2),COLUMNS($F2:F2))),"")


For E2 copy down
For F2 copy across and down

Hope it help too.
HOpe for feedback:)

This didn't work either. I get a #NAME error...maybe because I have excel 2007, not 2010?

I'm very grateful that 3 different people offered help on this. Thanks much everyone!
 
Last edited:
Upvote 0
Knockout punch.:)..
Try this one.
Excel Workbook
ABCDEFGHIJ
1Raw DataCountReturnValue
2a1a3123
3a2b14
4a3c256
5b4d478910
6c5
7c6
8d7
9d8
10d9
11d10
Sheet8
Excel 2010
Cell Formulas
RangeFormula
E2=COUNTIF($A$2:$A$11,D2)
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
 
Upvote 0
Knockout punch.:)..
Try this one.
Excel Workbook
ABCDEFGHIJ
1Raw DataCountReturnValue
2a1a3123
3a2b14
4a3c256
5b4d478910
6c5
7c6
8d7
9d8
10d9
11d10
Sheet8
Excel 2010
Cell Formulas
RangeFormula
E2=COUNTIF($A$2:$A$11,D2)
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.

That works. I appreciate the help. Macro is probably easier overall, but I wasn't familiar with some of the functions you used here, so I still learned something :)

Thanks!
 
Upvote 0
Code:
Sub transpose()
last = Cells(Rows.Count, "C").End(xlUp).Row
lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lr
    With WorksheetFunction.Application
        If Not IsError(.Index(Range("C2:C" & last), _
            .Match(Range("A" & i), Range("C2:C" & last), 0), 1)) = True Then
            Set D_Rng = Range("D" & .Match(Range("A" & i), Range("C2:C" & last), 0) + 1)
            D_Rng.Value = D_Rng & "--" & Range("B" & i)
        Else
            Range("C" & last + 1).Value = Range("A" & i)
            Range("D" & last + 1).Value = Range("B" & i)
            last = last + 1
        End If
    End With
Next
Columns("A:B").Delete Shift:=xlToLeft
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

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