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

如何在Excel/VBA中提取三列共同重复值?

从Excel三列提取共同存在的值

方法一:Excel公式(适用于Excel 365/2021及以上版本)

假设数据区域为A2:C6(表头在A1:C1),在D2单元格输入以下公式,回车后自动生成结果:

=UNIQUE(FILTER(A2:A6,COUNTIFS(B2:B6,A2:A6,C2:C6,A2:A6)=1))
  • COUNTIFS(B2:B6,A2:A6,C2:C6,A2:A6):统计A列每个值同时在B列和C列出现的次数
  • FILTER:筛选出计数为1的项(即三列均存在的值)
  • UNIQUE:自动去除结果中的重复值(若原数据有重复的共同值)

如果你的Excel版本没有FILTER/UNIQUE函数(如2019及更早版本),可以用数组公式:
在D2单元格输入以下公式后,按Ctrl+Shift+Enter组合键确认,再下拉填充直到出现空值:

=IFERROR(INDEX(A:A,SMALL(IF(COUNTIF(B:B,A:A)*COUNTIF(C:C,A:A)>0,ROW(A:A),""),ROW(A1))),"")

方法二:VBA宏

如果需要批量处理或更灵活的操作,可通过VBA实现:

  1. 按Alt+F11打开VBA编辑器
  2. 右键点击当前工作表,选择「插入」→「模块」
  3. 粘贴以下代码:
Sub ExtractCommonValues()
    Dim ws As Worksheet
    Dim lastRowA As Long, lastRowB As Long, lastRowC As Long
    Dim i As Long, dRow As Long
    Dim val As String
    Dim existingVals As Collection
    
    Set ws = ActiveSheet
    Set existingVals = New Collection '用于去重
    
    lastRowA = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    lastRowB = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    lastRowC = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    dRow = 2 'D列从第2行开始
    
    '清空D列旧数据
    ws.Range("D2:D" & ws.Cells(ws.Rows.Count, "D").End(xlUp).Row).ClearContents
    
    On Error Resume Next '忽略重复添加的错误
    For i = 2 To lastRowA
        val = ws.Cells(i, "A").Value
        '检查值是否在B、C列都存在,且未添加过
        If val <> "" Then
            If Not ws.Range("B2:B" & lastRowB).Find(val, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing _
               And Not ws.Range("C2:C" & lastRowC).Find(val, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
                existingVals.Add val, Key:=CStr(val)
            End If
        End If
    Next i
    On Error GoTo 0
    
    '将去重后的结果写入D列
    For i = 1 To existingVals.Count
        ws.Cells(dRow + i - 1, "D").Value = existingVals(i)
    Next i
End Sub
  1. 返回Excel,按Alt+F8选择ExtractCommonValues宏并运行,结果会自动写入D列。

内容的提问来源于stack exchange,提问作者user22667330

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 18:54:51