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.
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.
Comments