Excelmacros – 连续父列表中连续的子列表计数

请帮助我连续列出父母名单中的连续子女名单。
我有一个数据表:
—– < – A(列) – > < – B(列) – > < – C(列) – >
1 —– aa ————— aa —————- aa
2 —– bb ————— bb —————- bb
3 —– aa ———————————— aa
4 —– aa
5 —– bb
6 —– bb
7 —– aa
8 —– aa
9 —– aa
10 —- bb
11 —- aa
12 —- bb

如果我列表A列表B,返回的结果是4。
如果我列表A中的列表C,返回的结果是2。
感谢您的帮助。

这几乎是一个蛮力的解决scheme,但会做的伎俩。

Option Explicit Public Function PatternCount(PatternRange As Excel.Range, TargetRange As Excel.Range) As Integer Dim oCell As Excel.Range Dim oEndCell As Excel.Range Dim sTargetPattern As String Dim sPattern As String Dim iCount As Integer sTargetPattern = RangeSignature(PatternRange) For Each oCell In TargetRange Set oEndCell = oCell.Offset(RowOffset:=PatternRange.Count - 1) sPattern = RangeSignature(Range(oCell, oEndCell)) If sPattern = sTargetPattern Then iCount = iCount + 1 Next oCell PatternCount = iCount Set oCell = Nothing Set oEndCell = Nothing End Function Private Function RangeSignature(TargetRange As Excel.Range) As String Dim oCell As Excel.Range Dim sPattern As String For Each oCell In TargetRange.Cells sPattern = sPattern & oCell.Value & "|" Next oCell RangeSignature = sPattern Set oCell = Nothing End Function