gpt4 book ai didi

Excel - 合并具有共同值的行并将差异连接在一列中

转载 作者:行者123 更新时间:2023-12-02 11:44:12 25 4
gpt4 key购买 nike

我想合并具有共同值的行并将差异连接在一列中。

我认为最简单的方法就是向您展示一个示例。

输入:

Customer Name   |   NEW YORK    |   ALBANY 
Customer Name | NEW YORK | CLINTON
Customer Name | NEW YORK | COLUMBIA
Customer Name | NEW YORK | DELAWARE
Customer Name | NEW YORK | DUTCHESS
Customer Name | VERMONT | BENNINGTON
Customer Name | VERMONT | CALEDONIA
Customer Name | VERMONT | CHITTENDEN
Customer Name | VERMONT | ESSEX
Customer Name | VERMONT | FRANKLIN

所需输出:

Customer Name   |   VERMONT     |   BENNINGTON,CALEDONIA,CHITTENDEN,ESSEX,FRANKLIN
Customer Name | NEW YORK | ALBANY,CLINTON,COLUMBIA,DELAWARE,DUTCHESS

我确实看到了一些其他关于此的帖子,但我认为它们并不正是我想要做的。

最佳答案

如果 | 您指的是单独的单元格,则以下宏 (Excel 2007) 应该可以解决问题(您的数据从单元格 A1 开始):

Application.ScreenUpdating = False

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

'first: make sure data is sorted
Sort.SortFields.Clear
Sort.SortFields.Add Key:=Columns("A:A"), SortOn:=xlSortOnValues
Sort.SortFields.Add Key:=Columns("B:B"), SortOn:=xlSortOnValues
Sort.SortFields.Add Key:=Columns("C:C"), SortOn:=xlSortOnValues

With Sort
.SetRange Range("A1:C" & last_row)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

'then: join text until key values in two neighboring row changes
myText = ""
myPos = 1

For i = 1 To last_row
If Cells(i, 1).Value <> Cells(i + 1, 1).Value Or Cells(i, 2).Value <> Cells(i + 1, 2).Value Then
Cells(myPos, 5).Value = Cells(i, 1).Value
Cells(myPos, 6).Value = Cells(i, 2).Value

myText = myText & Cells(i, 3).Value
Cells(myPos, 7).Value = myText
myText = ""
myPos = myPos + 1
Else
myText = myText & Cells(i, 3).Value & ","
End If
Next i

Application.ScreenUpdating = True
MsgBox "Done"

关于Excel - 合并具有共同值的行并将差异连接在一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com