提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何将重复的行转换为Excel中的列?

假设您在Excel中有一系列数据,现在您想将重复行转置为多列,如下图所示,您是否有任何好主意来解决此任务?

用VBA代码转置重复行到列

DOC-转换,复制,行,列,1


将一列或一行转换为一个范围,反之亦然:
您订购的 Kutools for Excel 变换范围 功能,可以快速将一系列单元格转换为单个行或一列,还可以将单个行或列转换为单元格范围。 阅读更多关于这个功能...
doc转置范围

箭头蓝色右泡 用VBA代码转置重复行到列


不幸的是,在Excel中没有直接的方式来处理它,但是你可以创建一个VBA代码来解决它,请按如下操作:

1. 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,并将以下代码粘贴到 模块窗口.

VBA代码:将重复行转置为多列

Sub ConvertTable()
'Update 20150113
Dim xArr1 As Variant
Dim xArr2 As Variant
Dim InputRng As Range, OutRng As Range
Dim xRows As Long
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
xArr1 = InputRng.Value
t = UBound(xArr1, 2): xRows = 1
With CreateObject("Scripting.Dictionary")
    .CompareMode = 1
    For i = 2 To UBound(xArr1, 1)
        If Not .exists(xArr1(i, 1)) Then
            xRows = xRows + 1: .Item(xArr1(i, 1)) = VBA.Array(xRows, t)
            For ii = 1 To t
                xArr1(xRows, ii) = xArr1(i, ii)
            Next
        Else
            xArr2 = .Item(xArr1(i, 1))
            If UBound(xArr1, 2) < xArr2(1) + t - 1 Then
                ReDim Preserve xArr1(1 To UBound(xArr1, 1), 1 To xArr2(1) + t - 1)
                For ii = 2 To t
                    xArr1(1, xArr2(1) + ii - 1) = xArr1(1, ii)
                Next
            End If
            For ii = 2 To t
                xArr1(xArr2(0), xArr2(1) + ii - 1) = xArr1(i, ii)
            Next
            xArr2(1) = xArr2(1) + t - 1: .Item(xArr1(i, 1)) = xArr2
        End If
    Next
End With
OutRng.Resize(xRows, UBound(xArr1, 2)).Value = xArr1
End Sub

3。 然后按 F5 键来运行这段代码,在弹出的对话框中选择想要将重复行转换为多列的数据范围,参见截图:

DOC-转换,复制,行,列,2

4。 点击 OK,然后在下面的对话框中选择一个您想要将结果放入的单元格,请参阅屏幕截图:

DOC-转换,复制,行,列,3

5. 然后点击 OK 按钮,您选择的数据已被转换为:

DOC-转换,复制,行,列,4


相关文章:

如何转置/转换成单行的行和列?

如何转置/转换成单列的行和列?

如何转置/转换单列到Excel中的多列?


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    arielle · 2 months ago
    Hello! Can anyone help me...

    I found this code to turn rows of data with a unique ID into a single row per ID with multiple columns (min column 5 - including ID, maximum columns 25 - including ID). Then it deletes all of the duplicate rows based on unique ID. This code sort of works, the only problem is it isn't transferring two columns of data.

    The number of rows of data per unique ID varies from 1 to 6 (therefore I would need minimum 5 columns to maximum 25 columns)

    There are 20,000 rows of data but I can break the data up by department for a minimum of 5,000 rows of data and run each department separately.

    Thanks for your help!


    My data looks something like this

    A B C D E
    ID DESCRIPTION STATE # DATE
    3 CPR US 567 6/19/2019
    3 AET US 568 6/19/2019
    4 CPR US 6/19/2019
    4 AET
    4 AED

    etc.

    I want it to look like this

    A B C D E F G H I J K L M ETC.....
    ID DESCRIPTION STATE # DATE DESCRIPTION STATE # DATE DESCRIPTION STATE # DATE
    3 CPR US 567 6/19/2019 AET US 568 6/19/2019
    4 CPR US AET US AED US

    Here is the code I found that sort of works (probably for what it was written for, it carries over only the data in columns D and E and omits column B & C... leaving two blank columns per data set. I like that it deletes the duplicates after moving all of the data to a single column based on unique ID

    Sub Addresses_To_Columns()

    Dim lastRow As Long
    Dim addressCount As Integer: addressCount = 0

    lastRow = Range("A" & Rows.Count).End(xlUp).Row

    For i = lastRow To 3 Step -1
    If Cells(i, 1).Value = Cells(i - 1, 1).Value Then
    addressCount = addressCount + 1
    Range(Cells(i - 1, 10), Cells(i - 1, (addressCount * 5) + 10)) = Range(Cells(i, 4), Cells(i, (addressCount * 5) + 4)).Value
    Rows(i).Delete
    Else
    addressCount = 0
    End If
    Next i

    End Sub
  • To post as a guest, your comment is unpublished.
    Joe · 1 years ago
    This works fine for me with different number of duplicates Brian. I only had a problem with the first duplicate showing twice on my output, but that was very minor. I only wish I knew how to make it copy the duplicate results into a comma delimited format instead of new columns for each one.
  • To post as a guest, your comment is unpublished.
    Brian · 1 years ago
    Only works if there's the same number of duplicates and call him a. It doesn't work at if have different numbers of duplicates.
  • To post as a guest, your comment is unpublished.
    Alex · 2 years ago
    Really great. Saved me a lot of time
  • To post as a guest, your comment is unpublished.
    Jon · 3 years ago
    I am so incredibly happy this worked. You are my hero!!!!
  • To post as a guest, your comment is unpublished.
    Santosh · 4 years ago
    Awesome Example - saved me a lot of time.
    Thank You so much !!!