如何在Excel中合并关联产品附加代码至Col9(逗号分隔)
报表格式转换解决方案
需求说明
需将现有报表中产品行后的附加代码合并到对应产品行的Col9列,以逗号分隔;附加代码所在行的Col1为空,代码存储在Col4和Col5中。支持去除重复代码,保留重复也可接受。
原报表示例
| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8 | Col9 |
|---|---|---|---|---|---|---|---|---|
| 53465 | 517 | Brand1 | 083758314483 | 08375831448 | 044800 | 044800 | Supp1 | |
| 88565 | 517 | Brand2 | 08375801599 | 08375801599 | 1599 | Supp2 | ||
| 083758015991 | 5032501599 | Supp2 | ||||||
| 88566 | 517 | Brand2 | 08375801799 | 08375801799 | 1799 | Supp2 | ||
| 083758017995 | 83758317996 | Supp2 | ||||||
| 88567 | 517 | Brand2 | 08375801999 | 08375801999 | 1999 | Supp2 | ||
| 083758019999 | 5032501999 | Supp2 | ||||||
| 75239 | 517 | Brand2 | 83758420009 | 083758420009 | 322200 | Supp2 | ||
| 083758432163 | 83758432163 | Supp2 | ||||||
| 083758432187 | 83758432187 | Supp2 | ||||||
| 083758432279 | 83758432279 | Supp2 | ||||||
| 083758420009 | 83758432262 | Supp2 | ||||||
| 53478 | 517 | Brand3 | 083758298547 | 08375829854 | 085400 | 085400 | Supp2 | |
| 53479 | 517 | Brand3 | 083758298554 | 08375829855 | 085500 | 085500 | Supp2 |
目标格式示例
| Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8 | Col9 |
|---|---|---|---|---|---|---|---|---|
| 53465 | 517 | Brand1 | 083758314483 | 08375831448 | 044800 | 044800 | Supp1 | |
| 88565 | 517 | Brand2 | 08375801599 | 08375801599 | 1599 | Supp2 | 083758015991,5032501599 | |
| 88566 | 517 | Brand2 | 08375801799 | 08375801799 | 1799 | Supp2 | 083758017995,83758317996 | |
| 88567 | 517 | Brand2 | 08375801999 | 08375801999 | 1999 | Supp2 | 083758019999,5032501999 | |
| 75239 | 517 | Brand2 | 83758420009 | 083758420009 | 322200 | Supp2 | 083758432163,83758432163,083758432187,83758432187,083758432279,83758432279,083758420009,83758432262 | |
| 53478 | 517 | Brand3 | 083758298547 | 08375829854 | 085400 | 085400 | Supp2 | |
| 53479 | 517 | Brand3 | 083758298554 | 08375829855 | 085500 | 085500 | Supp2 |
解决方案
方法一:Excel公式+筛选(适合Excel 2019/365)
- 添加辅助列(Col10):在J2单元格输入公式
=IF(A2<>"",ROW(),J1),下拉填充至所有行,将附加行关联到最近的产品行号。 - 合并代码到Col9:在产品行的I2单元格输入数组公式
=TEXTJOIN(",",TRUE,IF($J$2:$J$15=ROW(),$D$2:$E$15,"")),按Ctrl+Shift+Enter完成输入(Excel 365直接回车),下拉填充所有产品行。 - 清理数据:筛选Col1不为空的行,复制到新工作表,删除辅助列即可。
- 如需去重,修改公式为:
=TEXTJOIN(",",TRUE,UNIQUE(FILTER($D$2:$E$15,$J$2:$J$15=ROW(),"")))
方法二:Excel VBA宏(适合批量/旧版Excel)
按Alt+F11打开VBA编辑器,插入模块并粘贴以下代码,运行后自动完成合并和清理:
Sub MergeAdditionalCodes() Dim lastRow As Long Dim i As Long Dim currentProductRow As Long Dim codeList As String lastRow = Cells(Rows.Count, "A").End(xlUp).Row currentProductRow = 1 For i = 1 To lastRow If Cells(i, "A").Value <> "" Then If currentProductRow <> i Then Cells(currentProductRow, "I").Value = Left(codeList, Len(codeList) - 1) codeList = "" End If currentProductRow = i Else If Cells(i, "D").Value <> "" Then codeList = codeList & Cells(i, "D").Value & "," If Cells(i, "E").Value <> "" Then codeList = codeList & Cells(i, "E").Value & "," End If Next i If codeList <> "" Then Cells(currentProductRow, "I").Value = Left(codeList, Len(codeList) - 1) For i = lastRow To 1 Step -1 If Cells(i, "A").Value = "" Then Rows(i).Delete Next i End Sub
方法三:Python脚本(适合大数据量)
安装pandas库后,运行以下脚本处理:
import pandas as pd # 读取报表(支持Excel/CSV,替换为你的文件路径) df = pd.read_excel("报表文件.xlsx") # 关联附加行到对应产品行 df["product_id"] = df["Col1"].ffill() # 收集每个产品的附加代码 def collect_codes(group): codes = [] for _, row in group.iterrows(): if pd.isna(row["Col1"]): if not pd.isna(row["Col4"]): codes.append(str(row["Col4"])) if not pd.isna(row["Col5"]): codes.append(str(row["Col5"])) # 可选去重:codes = list(set(codes)) return ",".join(codes) code_groups = df.groupby("product_id").apply(collect_codes).reset_index(name="Col9") # 合并结果并保存 result = df[df["Col1"].notna()].merge(code_groups, left_on="Col1", right_on="product_id", how="left") result["Col9"] = result["Col9_y"] result = result.drop(columns=["product_id", "Col9_y"]) result.to_excel("格式化后报表.xlsx", index=False)
内容的提问来源于stack exchange,提问作者FRG Tech
相关产品推荐
相关产品推荐

