두 열을 기반으로 중복 행을 찾아 결과를 다른 워크시트로 전송

두 열을 기반으로 중복 행을 찾아 결과를 다른 워크시트로 전송

B열과 D열에 가끔 중복되는 데이터가 포함된 워크시트("Saisie de Données")가 있습니다. 이러한 중복 항목을 인식하고 G 열에서 V 열의 데이터를 합산하고 싶습니다. 그런 다음 결과는 관련 데이터가 포함된 중복되지 않은 행을 수신하는 다른 워크시트("Sommaire - Paie")로 전송됩니다. 합계 결과가 있는 중복 행. 새 워크시트에 복사되지 않는 열 C를 제외하고 두 워크시트 간에 모든 열은 동일하게 유지됩니다. 매크로가 실행될 때마다 두 번째 워크시트("Sommaire - Paie")의 데이터가 덮어쓰여집니다.

분석할 데이터("Saisie de Données")와 내가 수동으로 생성한 예상 결과("Sommaire - Paie")가 포함된 워크시트의 사본을 첨부했습니다.

첨부파일을 받으시려면 다음을 따르세요.이 드롭박스 링크.

실제 통합 문서에는 훨씬 더 많은 줄이 있지만 패턴은 항상 동일합니다. 즉, 직원의 이름과 주중에 근무한 시간이 표시됩니다.

답변1

Yoyo Jiang이라는 사람으로부터 도움을 받았는데 코드가 완벽하게 작동합니다. 내가 사용한 코드는 다음과 같습니다.

Private Sub TestSumDuplicate()

Dim WS1 As Worksheet
Dim WS2 As Worksheet
Set WS1 = ThisWorkbook.Worksheets("Saisie de Données")
Set WS2 = ThisWorkbook.Worksheets("Sommaire - Paie (2)")

Dim oRange1 As Range
Dim oRange2 As Range
Dim tempRange As Range

Set oRange2 = WS2.Range("A29", "U110")
oRange2.ClearContents
Set oRange1 = WS1.Range("A30", "V553")

Dim i As Integer
Dim j As Integer
Dim t As Integer
Dim m As Integer
Dim n As Integer

Dim bFlag As Boolean

' j to record the current relative row location in oRange2
j = 1

For i = 0 To oRange1.Rows.Count - 1

bFlag = False '' to record if there is already a same category in oRange2.

If Not oRange1.Cells(i, 2) = "" Then
If Not oRange1.Cells(i, 2) = "Ligne Sommaire" Then

'' If it a row need to be check
For t = 1 To j
If oRange2.Cells(t, 3) = oRange1.Cells(i, 4) And oRange2.Cells(t, 2) = oRange1.Cells(i, 2) Then
bFlag = True

    '' Sum if duplicate
    For m = 0 To 18
    If Not oRange1.Cells(i, 7 + m) = "" Then
        oRange2.Cells(t, 6 + m) = oRange1.Cells(i, 7 + m) + oRange2.Cells(t, 6 + m)
    End If
    Next m

Exit For
End If
Next t

If bFlag = True Then
    bFlag = False
Else
    '' doesn't find a duplicate value
    oRange2.Cells(j, 1) = oRange1.Cells(i, 1)
    oRange2.Cells(j, 2) = oRange1.Cells(i, 2)

    For m = 4 To 25
     oRange2.Cells(j, m - 1) = oRange1.Cells(i, m)
    Next m

    j = j + 1
End If

End If
End If
Next i

End Sub

관련 정보