How can I remove duplicates by searching on a rows for a range of collumns until the last row with text in VBA?

Ioana Vrabie

I need to write a macro that would remove duplicate words from columns J-P if they appear in the D, E, F, G columns. Also, the keywords will need to be removed if they appear twice in the J-P range. This is how the data looks like

In the attached picture the word "carta" from cell J2 needs to be removed because it is already present in E2, it's the same same for the part_number. Also the word "scuola" appears twice (cells M2 and O2) so it should be removed. Also, I will need the macro to run for all the rows it finds in cell A and to color the empty cells after removing in red so the user will know that a keyword is missing after running the marco.

Please let me know if you have any suggestions I can try as I am quite new to VBA.

FaneDuru

Please, try the next code. It should be very fast, using arrays and all processing being done in memory. Finally it drops the processing result at once. For testing reasons it returns the result starting from "R2". In this way you can compare the result with what you want accomplishing. If everything looks to be as you need, you can simply adapt the last code line, only changing sh.Range("R2") with sh.Range("J2") and your existing content will be overwritten by the processed array content:

Sub MatchDeleteDuplicates()
 Dim sh As Worksheet, arr, arrFin, lastRow As Long, i As Long, j As Long, k As Long
 Dim strSearch As String, rngModif As Range
 
 Set sh = ActiveSheet                      'please, use here your sheet to be processed
 lastRow = sh.Range("A" & rows.count).End(xlUp).row 'last row in column A:A
 
 arr = sh.Range("D2:G" & lastRow).Value    'place the range content in an array
 arrFin = sh.Range("J2:P" & lastRow).Value 'place the range content in an array
 For i = 1 To UBound(arr)  'iterate between the arrays elements and change the arrFin content
    For j = 1 To UBound(arrFin, 2)
        strSearch = arrFin(i, j)
        For k = 1 To UBound(arr, 2)
            'search a match in D:G columns:
            If InStr(1, arr(i, k), strSearch, 1) > 0 Then
                arrFin(i, j) = ""
                If rngModif Is Nothing Then
                    Set rngModif = sh.cells(i + 1, j + 9) 'create the range to finally color its interior
                Else
                    Set rngModif = Union(rngModif, sh.cells(i + 1, j + 9))
                End If
                Exit For
            End If
        Next k
        'search duplicates in J:P columns:
        For k = j + 1 To UBound(arrFin, 2)
            If arrFin(i, k) = strSearch Then
                arrFin(i, k) = ""
                If rngModif Is Nothing Then
                    Set rngModif = sh.cells(i + 1, k + 9) 'create the range to finally color its interior
                Else
                    Set rngModif = Union(rngModif, sh.cells(i + 1, k + 9))
                End If
                Exit For
            End If
        Next k
    Next j
 Next i
 ' drop the arrFin content at once (very fast):
 sh.Range("R2").Resize(UBound(arrFin), UBound(arrFin, 2)).Value = arrFin
 ' color interior of the modified range:
 If Not rngModif Is Nothing Then rngModif.Interior.Color = vbRed
End Sub

Take care to have in column A:A, the same number of rows like in columns to be processed...

I would suggest you to test the code on some rows and if everything looks good, test it on whole existing range. It should be fast, too, but not having something similar to test, please send some feedback.

Edited:

Now the code color modified cells interior in red.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove duplicates by searching on a rows for a range of columns until the last row with text in VBA?

how can I remove some rows that are partial duplicates in r?

Remove rows in a group by until the last row meets some condition

How do I make my SumIf Function run through until last range of my data on VBA Code

MS Access / VBA - How can I split rows in a Date Range

How can I assign a dynamic range (of rows) to a checkbox in vba?

Mismatch between Range.Rows.Count and the rows I can return by passing row index to Range.Rows in Excel 2010 VBA

How to remove last row from list of rows

How do I remove duplicates row in SQL?

How can I remove last 2 lines in text file in Java?

How can I remove text on the last line of a file?

For Loop - How Can I Set The Range For To Be From The Active Row Down To Last Row?

how can i remove buttons and text like row in css

How can I get a count for duplicates in SQL Server and write the result for each row to a column (without excluding rows)?

How can I remove same words in both columns? Duplicates in both rows

How can I remove duplicates in an array of object?

How can I remove duplicates sorted by traits?

How can I remove any rows that have missing data within a specific range of columns and on only specific rows?

Excel VBA Range until last available cell

Google Script - Remove duplicates in last 15 rows

MYSQL counting rows until last different row

How do I hide a range of rows by getting the start and end row values from user input cells into the range VBA code

How to locate the last row of cells in a range which matches using VBA?

How can I remove duplicate rows and reversed-order duplicate rows in VBA-Excel

How can I possibly get the last row from a range of a sheet using apps script?

how can I get the row number of the last used cell in a given range

How do I remove the rows of duplicates based on a "key" column?

How can I group and select 1 row or if there's more than 1 just the last rows with a SQL SELECT

How Can I delete the last row from the table in a form with out other rows with inserted data?