gpt4 book ai didi

EXCEL:合并多行宏

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

我需要一个宏来查看 COL A 的所有实例,并将 COL B 的所有值合并到一行中,同时删除该过程中的重复项。添加逗号是一个优点。

我不懂任何VBA,但如果有人愿意解释,我很乐意学习。这不是我需要的第一个 VBA 解决方案。谢谢!

我需要的示例:

COL A    COL B 
100 ---- PC 245
100 ---- PC 246
100 ---- PC 247
101 ---- PC 245
101 ---- PC 246
101 ---- PC 247

进入

COL A    COL B 
100 ---- PC 245, PC 246, PC 247
101 ---- PC 245, PC 246, PC 247

此数据将进入 map ,因此我需要将其连接到工具提示文本。任何帮助表示赞赏。谢谢!

PS:我需要的是一个宏。我不需要的是数据透视表。

最佳答案

重新发布此代码,因为它已被版主删除。 @bill-the-lizard,在重新删除它之前,您能评论一下我的答案有什么问题吗?

Sub ConsolidateRows()
'takes rows and consolidate one or many cells, based on one or many cells matching with above or below rows.

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

'**********PARAMETERS TO UPDATE****************
Const strMatch As String = "A" 'columns that need to match for consolidation, separated by commas
Const strConcat As String = "B" 'columns that need consolidating, separated by commas
Const strSep As String = ", " 'string that will separate the consolidated values
'*************END PARAMETERS*******************

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

colMatch = Split(strMatch, ",")
colConcat = Split(strConcat, ",")

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

For i = lastRow To 2 Step -1 'loop from last Row to one

For j = 0 To UBound(colMatch)
If Cells(i, colMatch(j)) <> Cells(i - 1, colMatch(j)) Then GoTo nxti
Next

For j = 0 To UBound(colConcat)
Cells(i - 1, colConcat(j)) = Cells(i - 1, colConcat(j)) & strSep & Cells(i, colConcat(j))
Next

Rows(i).Delete

nxti:
Next

application.ScreenUpdating = True 'reenable ScreenUpdating
End Sub

关于EXCEL:合并多行宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353080/

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