【问题标题】:How to transpose multiple cells in row to column in Excel [duplicate]如何在Excel中将行中的多个单元格转置为列[重复]
【发布时间】:2017-02-20 05:20:38
【问题描述】:

我的数据看起来像:

name    field1  field2  field3
John    abc     dcf     fdd
Sue     ddf     
Beth    fds     rtfds   

我希望它看起来像这样:

abc     John
dcf     John
fdd     John
ddf     Sue
fds     Beth
rtfds   Beth

注意行有可变数量的 fieldx 列...我正在尝试使用 VBA script from MrExcel 并得到“应用程序定义或对象定义错误”。

有没有办法只用一个excel公式来做到这一点?我可以发布 VBA 脚本,但如果有公式解决方案会更好。

【问题讨论】:

标签: excel transpose vba


【解决方案1】:

假设您的数据从Sheet1 的单元格A1 开始,并且您想转置为Sheet2,则如下所示:

Sub test()

    Dim s1 As Worksheet
    Dim s2 As Worksheet
    Dim lCol As Long
    Dim i As Long
    Dim lRow As Long

    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")

    With s1

        lRow = .Cells(.Rows.Count, 1).End(xlUp).Row

        For i = 2 To lRow

            lCol = .Cells(i, .Columns.Count).End(xlToLeft).Column

            .Range(.Cells(i, 2), .Cells(i, lCol)).Copy

            s2.Cells(1, i - 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                False, Transpose:=True

        Next i

    End With

End Sub

【讨论】:

  • 这不会产生 OP 想要的结果。
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多