You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Excel中合并关联产品附加代码至Col9(逗号分隔)

报表格式转换解决方案

需求说明

需将现有报表中产品行后的附加代码合并到对应产品行的Col9列,以逗号分隔;附加代码所在行的Col1为空,代码存储在Col4和Col5中。支持去除重复代码,保留重复也可接受。

原报表示例

Col1Col2Col3Col4Col5Col6Col7Col8Col9
53465517Brand108375831448308375831448044800044800Supp1
88565517Brand208375801599083758015991599Supp2
0837580159915032501599Supp2
88566517Brand208375801799083758017991799Supp2
08375801799583758317996Supp2
88567517Brand208375801999083758019991999Supp2
0837580199995032501999Supp2
75239517Brand283758420009083758420009322200Supp2
08375843216383758432163Supp2
08375843218783758432187Supp2
08375843227983758432279Supp2
08375842000983758432262Supp2
53478517Brand308375829854708375829854085400085400Supp2
53479517Brand308375829855408375829855085500085500Supp2

目标格式示例

Col1Col2Col3Col4Col5Col6Col7Col8Col9
53465517Brand108375831448308375831448044800044800Supp1
88565517Brand208375801599083758015991599Supp2083758015991,5032501599
88566517Brand208375801799083758017991799Supp2083758017995,83758317996
88567517Brand208375801999083758019991999Supp2083758019999,5032501999
75239517Brand283758420009083758420009322200Supp2083758432163,83758432163,083758432187,83758432187,083758432279,83758432279,083758420009,83758432262
53478517Brand308375829854708375829854085400085400Supp2
53479517Brand308375829855408375829855085500085500Supp2

解决方案

方法一:Excel公式+筛选(适合Excel 2019/365)

  1. 添加辅助列(Col10):在J2单元格输入公式 =IF(A2<>"",ROW(),J1),下拉填充至所有行,将附加行关联到最近的产品行号。
  2. 合并代码到Col9:在产品行的I2单元格输入数组公式 =TEXTJOIN(",",TRUE,IF($J$2:$J$15=ROW(),$D$2:$E$15,"")),按Ctrl+Shift+Enter完成输入(Excel 365直接回车),下拉填充所有产品行。
  3. 清理数据:筛选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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 20:30:57