2011-03-24 4 views
1

연속 된 상위 목록에서 연속 된 하위 목록을 계산하도록 도와주세요.
난 데이터 테이블이 - A (열) -> < - B (열) -> < - C (열) ->
1 ----- ----- AA
<을 - ------------- aa ---------------- aa
2 ----- bb ---------- ----- bb ---------------- bb
3 ----- aa ------------------ ------------------
AA AA 4 ----- ----- 5
BB
6 ----- BB
7 - ---- aa
8 ----- aa

----- AA BB
10 ---- 11 ---- 나리스트 (A)에리스트 B를 계산하는 경우, 결과를 리턴
12 ---- BBExcel 매크로 - 연속적인 상위 목록에서 연속 하위 목록 계산

AA는 4이다.
목록 A의 목록 C를 계산하면 반환 된 결과는 2입니다.
도움 주셔서 감사합니다.

+0

B/C 유형 열의 깊이에는 최대 행 수가 있습니까? –

답변

1

이것은 꽤 힘든 해결책이지만 트릭을 수행 할 것입니다.

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 
+0

니스! 중복 된 횟수는 계산됩니다. 'a | b | a | b | a | b | a | b | a | b | a | b'에서 'a | b | a | b'를 찾으면, 'PatternCount'는 3이 아닌 5를 반환합니다. 당신은 기대할 수도 있고 기대하지 않을 수도 있습니다. –