how to change the macro ... It is necessary to find duplicates and write a comment in the next cell
There is no need to refine, but to change the approach. First, fill in conditional formatting, then try to determine duplicates using this formatting ...
Simpler. According to the values we go ... and we find :)
Work with leaf objects is slow, therefore it is better to process in memory.
Check the values of column D. In column E, write the row numbers of duplicates found.
Sub Tst() Dim Ar() Dim lRws As Long Dim i As Long, k As Long With ActiveSheet lRws = .Cells(.Rows.Count, 4).End(xlUp).Row ' последняя заполненная ячейка Ar = .Range("D1:D" & lRws).Value ' значения в массив ReDim Preserve Ar(1 To lRws, 1 To 2) ' расширяем размерность для комментариев For i = 1 To lRws - 1 ' цикл по проверяемым значениям If Ar(i, 1) <> Empty Then ' значение есть For k = i + 1 To lRws ' цикл по сравниваемым значениям If Ar(i, 1) = Ar(k, 1) Then ' нашли дубль ' записываем комментарий - номера строк с дублями Ar(i, 2) = Ar(i, 2) & "/" & k Ar(k, 2) = Ar(k, 2) & "/" & i End If Next k End If Next i .Range("D1:E" & lRws).Value = Ar ' выгрузка на лист End With End Sub
The code below will mark duplicate values:
Sub SearchOfTakes() Dim rRng As Range, c As Range Dim oDict As Object Dim lRws As Long With ActiveSheet lRws = .Cells(.Rows.Count, 4).End(xlUp).Row ' последняя заполненная ячейка Set rRng = .Range("D1:D" & lRws) ' диапазон в переменную Set oDict = CreateObject("scripting.dictionary") ' словарь For Each c In rRng ' цикл по ячейкам диапазона If c.Value <> "" Then ' значение есть If oDict.exists(c.Value) Then ' если значение было раньше c.Offset(, 1) = "дубль" ' отмечаем его Else ' значение первый раз в диапазоне Set oDict(c.Value) = c ' в словарь End If End If Next c End With End Sub