Excel-有条件地将行转置为列

时间:2019-05-30 06:18:08

标签: excel excel-formula

我已经提取了一份在线电子表格的摘录,其中包括:

1   date   author   modified   comment
1   date   author   modified   comment2
1   date   author   modified   comment3
1   date   author   modified   commentn
2   date   author   modified   comment
2   date   author   modified   comment2
2   date   author   modified   comment3
2   date   author   modified   commentn
3   date   author   modified   comment
3   date   author   modified   comment2
3   date   author   modified   comment3
3   date   author   modified   commentn
....
3000 date   author modified   comment60

与每个索引相关的评论数量各不相同,并且我们有数千行。 为了我的一生,我想不出如何用公式转置这些

是否有关于如何在Excel中实现此目标的想法?

我希望将其布局为:

1 author concat(date, comment)  concat(date, comment)  concat(date, comment)  
2 author concat(date, comment)  concat(date, comment)  concat(date, comment)  

...

2 个答案:

答案 0 :(得分:1)

这是使用某些公式的一种方法:

enter image description here

  • A2中的公式:

    =IFERROR(INDEX(Sheet1!$A$1:$A$12,MATCH(0,COUNTIF($A$1:A1,Sheet1!$A$1:$A$12),0)),"")
    

    通过 Ctrl Shift Enter

    输入

    向下拖动...

  • B2中的公式:

    =INDEX(Sheet!$C$1:$C$12,MATCH(A2,Sheet1!$A$1:$A$12,0))
    

    向下拖动...

  • C2中的公式:

    =IFERROR(INDEX(Sheet1!$B$1:$B$12,SMALL((Sheet1!$A$1:$A$12=$A2)*ROW(Sheet1!$A$1:$A$12),COUNTIF(Sheet1!$A$1:$A$12,"<>"&$A2)+(COLUMN()-2)))&", "&INDEX(Sheet1!$E$1:$E$12,SMALL((Sheet1!$A$1:$A$12=$A2)*ROW(Sheet1!$A$1:$A$12),COUNTIF(Sheet1!$A$1:$A$12,"<>"&$A2)+(COLUMN()-2))),"")
    

    通过 Ctrl Shift Enter

    输入

    左右拖动...

如果这将为您返回数字而不是日期,则可以将INDEX(Sheet1!$B$1:$B$12替换为INDEX(TEXT(Sheet1!$B$1:$B$12,"DD-MM-YYYY")或任何您想使用的日期格式。

您的第一步还可以仅是复制工作表1上ID的第一列,将其粘贴到工作表2上并删除重复项。在这种情况下,您将不得不调整其他公式的范围以A1而不是A2开头。

答案 1 :(得分:1)

您可以尝试:

Option Explicit

Sub test()

    Dim LastRowA As Long, i As Long, Count As Long, j As Long, k As Long, LastRowG As Long, LastColumn As Long, StartingPoint As Long
    Dim arr As Variant
    Dim strAuthor As String, strNextAuthor As String, strFullRecord As String

    With ThisWorkbook.Worksheets("Sheet1")

        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        arr = .Range("A1:E" & LastRowA)

        StartingPoint = 1

        For i = LBound(arr) To UBound(arr)

            If StartingPoint = i Then

                j = i + 1

                strAuthor = arr(i, 1)
                strNextAuthor = arr(j, 1)

                    Do Until strAuthor <> strNextAuthor

                        j = j + 1

                        If j > LastRowA Then
                            Exit Do
                        Else
                            strNextAuthor = arr(j, 1)
                        End If

                    Loop

                LastRowG = .Cells(.Rows.Count, "G").End(xlUp).Row

                If LastRowG = 1 And .Range("G1").Value = "" Then
                    LastRowG = 1
                Else
                    LastRowG = LastRowG + 1
                End If

                For k = i To j - 1

                    LastColumn = .Cells(LastRowG, .Columns.Count).End(xlToLeft).Column

                    If .Range("G" & LastRowG).Value = "" Then
                        .Range("G" & LastRowG).Value = arr(k, 1)
                        .Range("H" & LastRowG).Value = arr(k, 3)
                        .Range("I" & LastRowG).Value = "(" & arr(k, 2) & ", " & arr(k, 5) & ")"
                    Else
                        .Cells(LastRowG, LastColumn + 1) = "(" & arr(k, 2) & ", " & arr(k, 5) & ")"
                    End If

                Next k

                StartingPoint = j

            End If

        Next i

    End With

End Sub

结果:

enter image description here