lundi 18 octobre 2021

Findinf the right duplicate values in a column and cut paste the rows found duplicated according to 2 criterias (value and date)

I am stuck now writing a code to find duplicates in a cell sheet. What I am trying to do is as a first step to :

I have a list of data that fills up Column A to AQ and hundreds of rows. for first row is row 5, cause above it there is headers

I want to detect the rows that have the same value in column H (like duplicates values in cells in column H), then for those rows detected, check out 2 more variables. If the cell in the row in Column Q, and when it detects the row with similar value somewhere on the sheet, lets say it is on row 8 (but it could be anywhere in the sheet)that would be Q8), so then if Q8 = "vacant" (the word not empty cell), then I want to copy all the data in this row from A8 to AP8, and paste the row NEXT to the row that has the same value in column H (H5), so paste it in cells AQ5 to CF5. Then if Q8 >< is not equal to "Vacant", it should look to cell R8 (start date) and compare it with cell S5 (end date of first row it found) so it can make 2 decisions: If R8>S5, then copy paste the 2nd row it found row A8 to AP8 to CG5 to DV5 (of the first row it found) If R8<S5, then it should instead copy the first row it found row A5 to AP5 to CG 8 to DV8 (next to the second row)

Duplicates found can be up to 5 maybe. I wrote the following code below without copying (cutting) and pasting. Cause I want to see first the result, once I am confident in the code I will remove the =value and replace it with cut and paste.

But right now, I am still stuck, kind help appreciated:

Sub dup_cp()

Dim i As Integer Dim j As Integer Dim k As Integer

With Sheets("Vacant List") j = Application.CountA(.Range("A:A")) 'counts the number of filled in rows

For i = 5 To j
'it starts from line 5 on purpose, the numbers start from that line
    For k = i + 1 To j
        If .Cells(k, 8).Value = "Duplicate Value" Then GoTo skip_dup
        'it skips the line that has already been detected as duplicated

            If .Cells(k, 8).Value = .Cells(i, 8).Value Then
            'it finds the duplicate value in the eight column
            
                If .Cells(k, 17).Value = "VACANT" Then
                    .Range(.Cells(i, 43), .Cells(i, 84)).Value = .Range(.Cells(k, 1), .Cells(k, 42)).Value
                'it checks whether the cell i17 has the word "vacant" in it and then copy it in the middle range
            
                Else
                    If .Cells(k, 18).Value > .Cells(i, 19).Value Then
                'it compares the 18th column values (the modified duration of the components) and keeps the earlier date for comparison reasons
                        .Range(.Cells(i, 85), .Cells(i, 126)).Value = .Range(.Cells(k, 1), .Cells(k, 42)).Value
                    Else
                        .Range(.Cells(k, 85), .Cells(k, 126)).Value = .Range(.Cells(i, 1), .Cells(i, 42)).Value
                End If
            End If
        End If
        

skip_dup: Next Next End With End Sub

Aucun commentaire:

Enregistrer un commentaire