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

Excel技术需求:提取指定行中连续重复超7次的唯一值

Excel 查找连续重复超7次的唯一值解决方案

问题说明

需在Excel的B6:AE6行中,找出并显示满足连续重复超过7次条件的唯一值,且该方法要适配其他包含不同多值的行。当前尝试的公式如下:

=COUNT((MATCH(1,($B6:$Y6="1286")*($C6:$Z6="12286")*($D6:$AA6="12286")*($E6:$AB6="12286")*($F6:$AC6="12286")*($G6:$AD6="12286")*($H6:$AG6="12286"),0)))

可行解决方案

方法1:辅助列+动态数组公式(适配Excel 365/2021)

  1. 计算连续重复次数
    在空白列(如AF列)的AF6单元格输入公式,下拉覆盖目标行:

    =IF(B6=A6,AF5+1,1)
    

    逻辑:第一个单元格因A6为空显示1,后续单元格若与左侧值相同则次数累加,否则重置为1。

  2. 提取符合条件的唯一值
    在空白单元格输入动态数组公式,自动返回去重后的结果:

    =UNIQUE(FILTER(B6:AE6,AF6:AG6>7))
    

    逻辑:先用FILTER筛选出连续次数超7次的单元格值,再用UNIQUE去重得到唯一结果。

方法2:无辅助列数组公式(兼容旧版Excel)

若使用旧版Excel,输入以下数组公式后按Ctrl+Shift+Enter确认:

=INDEX(B6:AE6,MIN(IF(FREQUENCY(IF(B6:AE6=TRANSPOSE(B6:AE6),COLUMN(B6:AE6)),IF(B6:AE6<>TRANSPOSE(B6:AE6),COLUMN(B6:AE6)))>7,COLUMN(B6:AE6)-COLUMN(B6)+1)))

如需提取所有唯一值,可结合SMALL函数循环提取,或用VBA批量处理。

方法3:VBA宏批量处理多行

若需批量处理多行,使用以下VBA代码:

Sub FindConsecutiveDuplicates()
    Dim ws As Worksheet
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, j As Long, count As Integer
    Dim currentVal As Variant, result As Collection
    
    Set ws = ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    lastCol = ws.Cells(6, ws.Columns.Count).End(xlToLeft).Column
    
    For i = 6 To lastRow
        Set result = New Collection
        count = 1
        currentVal = ws.Cells(i, 2).Value
        
        For j = 3 To lastCol
            If ws.Cells(i, j).Value = currentVal Then
                count = count + 1
            Else
                If count > 7 Then
                    On Error Resume Next
                    result.Add currentVal, Key:=CStr(currentVal)
                    On Error GoTo 0
                End If
                currentVal = ws.Cells(i, j).Value
                count = 1
            End If
        Next j
        
        ' 检查最后一组连续值
        If count > 7 Then
            On Error Resume Next
            result.Add currentVal, Key:=CStr(currentVal)
            On Error GoTo 0
        End If
        
        ' 将结果写入该行最后一列右侧
        ws.Cells(i, lastCol + 1).Resize(1, result.Count).Value = GetArrayFromCollection(result)
    Next i
End Sub

Function GetArrayFromCollection(col As Collection) As Variant
    Dim arr() As Variant
    ReDim arr(1 To col.Count)
    
    For i = 1 To col.Count
        arr(i) = col(i)
    Next i
    
    GetArrayFromCollection = arr
End Function

使用方式:按Alt+F11打开VBA编辑器,插入模块粘贴代码后运行宏,结果会自动写入每行最后一列右侧。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 07:42:06