Excel Transpose like Rows

时间:2015-08-08 02:45:33

标签: excel

这应该是超级简单的,我只是无法想出找到它的关键字,但是:

有没有办法获取类似于:

的Excel工作表
a   1
a   2 
a   3
a   4
b   1 
b   2
c   1 
c   2 
c   3

并制作

a    1   2   3   4
b    1   2
c    1   2   3 

1 个答案:

答案 0 :(得分:0)

我也处于初学阶段,并希望与其他人分享我迄今为止的学习经历。请使用以下代码来解决您的问题。请根据您的要求手动添加标题行。

Sub Excel_Transpose_Like_Rows()

Dim lastRow As Long, i As Long, j As Long
Dim colMatch As Variant, colConcat As Variant

Application.ScreenUpdating = False 'disable ScreenUpdating to avoid screen flashes

lastRow = Range("A" & Rows.Count).End(xlUp).Row 'to get last row

For i = lastRow To 2 Step -1

    If Cells(i, 2) = Cells(i - 1, 2) Then
        Range(Cells(i, 3), Cells(i, Columns.Count).End(xlToLeft)).Copy Cells(i - 1, Columns.Count).End(xlToLeft).Offset(, 1)
        Rows(i).Delete
    Else
        If Cells(i, 1) = Cells(i - 1, 1) Then
            Range(Cells(i, 2), Cells(i, Columns.Count).End(xlToLeft)).Copy _
                Cells(i - 1, Columns.Count).End(xlToLeft).Offset(, 1)
            Rows(i).Delete
        End If
    End If

Next

Application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub