I have a code that finds duplicates. Can it also show the cell address for each…
Private research annotation
Sign in to add a private research label or note.
- User
I have a code that finds duplicates. Can it also show the cell address for each duplicate found: Sub CheckForDuplicates()
Dim dict As Object Set dict = CreateObject("Scripting.Dictionary") dict.CompareMode = vbTextCompare
Dim lastRow As Long lastRow = Cells(Rows.count, "J").End(xlUp).Row
Dim dupArray() As Variant Dim index As Long
ReDim dupArray(1 To lastRow) 'initialize the array
For i = 2 To lastRow ' start from row 2 Dim cellValue As String cellValue = Trim(Cells(i, "J").Value) If cellValue <> "" Then Dim vals() As String vals = Split(cellValue, " ") '''''''''If UBound(vals) >= 2 Then If UBound(vals) >= 1 Then Dim j As Long For j = 2 To UBound(vals) If dict.Exists(vals(j - 2) & " " & vals(j - 1) & " " & vals(j)) Then index = index + 1 ' increment index of dupArray dupArray(index) = Cells(i, "J").Value 'store duplicate in dupArray Exit For ' exit inner loop once a duplicate is found Else dict.Add vals(j - 2) & " " & vals(j - 1) & " " & vals(j), i End If Next j End If End If Next i
If index > 0 Then ' display duplicates if they are found ReDim Preserve dupArray(1 To index) 'adjust the size of dupArray MsgBox "Found duplicates: " & Join(dupArray, ", ") Else MsgBox "No duplicates found." End If
End Sub
- Assistant
Yes, you can modify the code to show the cell address for each duplicate found. You can add the cell address to the dupArray using the 'Address' property of the cell. Here's the modified code:
Sub CheckForDuplicates()
Dim dict As Object Set dict = CreateObject("Scripting.Dictionary") dict.CompareMode = vbTextCompare
Dim lastRow As Long lastRow = Cells(Rows.Count, "J").End(xlUp).Row
Dim dupArray() As Variant Dim addressArray() As Variant Dim index As Long
- User
Cell in column A24 is Blank, Cell in column B has the formula = A24. Column A is formatted as date 'dd/mm/yyyy' Column B is formated as custom of date 'dddd'. Even though cell A24 is blank, B24 is showing 'Saturday' How do I correct this
- Assistant
Even though cell A24 is blank, the formula in cell B24 is still trying to evaluate, which is causing it to return a value of "Saturday". You can modify the formula in cell B24 to only return a value if the corresponding cell in column A has a value. Here's how:
Change the formula in cell B24 to:
=IF(ISBLANK(A24),"",TEXT(A24,"dddd"))
This will first check if cell A24 is blank. If it is, it will return a blank value. If it's not, it will convert the value of A24 to a day name using the TEXT function.
This formula should ensure that cell B24 stays blank until a value is entered in cell A24.