2013-09-03 5 views
1

고객 이름 배열이 있습니다. 이 어레이는 중복으로 가득 차며 주문 행의 다른 데이터가 다를 수 있기 때문에 필요합니다. 이 데이터에 대한 고유 식별자가 없으므로 배열의 해당 고객을 보유하고있는 모든 행과 한 주문의 데이터를 비교해야합니다.중복 값이있는 배열에서 값 찾기

for 루프가 고객과 일치하는 모든 행을 검색하는 데 문제가 있습니다.

도움이 될 것입니다.

Dim prodlog As String 
Dim orddate As Variant 
Dim cus As String 
Dim owner As String 
Dim orddate2 As Variant 
Dim owner2 As String 
Dim LogCusts As Variant 
LogCusts = Application.Transpose(Range("F3", Range("F" & Rows.count).End(xlUp))) 
Dim loglen As Integer 
loglen = UBound(LogCusts) - LBound(LogCusts) 
Dim cust2 As Variant 
For Each cust2 In LogCusts 
    Dim custrow As Integer 
    custrow = Application.Match(cust2, LogCusts, False) + 1 
    prodlog = Range(Cells(custrow, 5), Cells(custrow, 5)).Value 
    orddate = Range(Cells(custrow, 2), Cells(custrow, 2)).Value 
    cus = Range(Cells(custrow, 6), Cells(custrow, 6)).Value 
    owner = Range(Cells(custrow, 7), Cells(custrow, 7)).Value 

    databook.Activate 
    logjam.Select 
    orddate2 = Range(Cells(custrow + 1, 5), Cells(custrow + 1, 5)).Value 
    owner2 = Range(Cells(custrow + 1, 7), Cells(custrow + 1, 7)).Value 

    If IsEmpty(orddate) Then 
     Exit For 
    End If 

    If IsEmpty(prodlog) Then 
     trackbook.Activate 
     masterlog.Select 
     Range(Cells(custrow, 2), Cells(custrow, 17)).Clear 
    Else: While cus = cust2 
     If orddate = orddate2 And owner = owner2 Then 
      Range(Cells(custrow, 8), Cells(custrow, 8)).Value = prodlog 
     End If 
    Wend 
End If 
Next cust2 
+1

다음은 Scripting.Dictionary를 사용하는 방법입니다. http://stackoverflow.com/a/11870350/2258 –

답변

0

은 당신이 원하는 일을 할 수있는 몇 가지 방법이 있습니다. 가장 쉬운 방법은 아마 @ 리차드 모건 (Richard Morgan)과 같은 사전을 사용하는 것입니다.

또한 배열을 순환하여 올바른 이름으로 새 배열을 만들 수도 있습니다.

샘플 코드 :

Private Sub UniqueArray(oldData as Variant) as Collection 
    Set UniqueArray = New Collection 

    Dim i as integer 
    Dim j as integer 
    Dim foundFlag as boolean 

    For i = 1 to oldData.Count 

    foundFlag = False 
    FOr j = i + 1 to oldData.Count 

     If oldData(i) = oldData(j) then 
     foundFlag = True 
     End If 
    Next j 

    If not foundFlag then UniqueArray.Add oldData(i) 
    Next 

가 다시 말하지만, 나는 사전을 사용 생각은 아마도 더 나은,하지만이 작동합니다.

0

와우 너무 복잡 보인다는 다음에 현재의 배열에 테이블을 만들려고 갖고 공식 사용

=IF(MAX(COUNTIF(A2:A11,A2:A11))>1,"Duplicates","No Duplicates") 

비가있는 경우 중복 표시 No Duplicates이있는 경우 Duplicates를 표시합니다. 물론 A2-A11에서.

아니면 한 번만 표시 값에 대한 항목에 대한

=COUNTIF($B$2:$B$11,B2)=1 

같은 것을에서 조건부 서식, 형식을 사용할 수 있습니다 정말 간단하게 유지합니다. 당신은 당신의 배열을위한 색 구성표를 얻기 위해 약간 수정할 수 있습니다. 그런 다음 어느 색상이 얼마나 많은 중복을 의미하는지 알려주는 측면의 키를 던집니다.

도움이 될지 모르겠지만 가능한 한 VBA에서 멀리하려고합니다.