在随机数字的列中搜索特定序列(Excel)

时间:2018-06-08 06:11:54

标签: excel excel-vba sequence vba

我有一个随机排列的列,例如。

range("a1:a30") = [1 48 8 93 6 2 5 1 7 43 ***10 1 300*** 4 2 5 1 11 2 3 1 3 2 1 1 3 2 6 5 4] 

在这些数据中,我想找到一个序列10 1 300并突出显示它。不应突出显示300 10 110 300 1等序列。

我尝试使用findconditional formattingmatch,但没有任何效果。我试过的所有这些函数返回单个值而不是序列。实际上,实际数据长达20000行,我无法使用目视检查来找到所需的序列。

编辑:将序列从1 2 3更改为10 1 300

2 个答案:

答案 0 :(得分:1)

Sub FindSequence()

    Dim rng As Range
    Dim lRow As Long 'the last row in column A with data
    Dim cRow As Long 'the current row for the loop below
    Dim sRow As Long 'start row of your sequence
    Dim eRow As Long 'end row of your saequence

    With Sheets("Data")
        lRow = Sheets("Data").Range("A" & .Rows.Count).End(xlUp).Row
    End With

    For cRow = 1 To lRow
        If Cells(cRow, 1).Value = 1 Then
            If Cells(cRow + 1, 1).Value = 2 Then
                If Cells(cRow + 2, 1).Value = 3 Then
                    sRow = cRow
                    eRow = cRow + 2
                    Sheets("Data").Range(Cells(sRow, 1), Cells(eRow, 1)).Interior.ColorIndex = 3
                End If
            End If
        End If
    Next cRow

End Sub

答案 1 :(得分:0)

在顶部插入两个银行行,清除所有格式,然后选择ColumnA,然后选择HOME>样式 - 条件格式,新规则...,使用公式确定要格式化的单元格格式化此公式的值:

=OR((A1048575=10)*(A1048576=1)*(A1=300),(A1048576=10)*(A1=1)*(A2=300),(A1=10)*(A2=1)*(A3=300))

格式化... ,选择格式选择,确定确定