So I incorporated this (with a few minor changes) into my accounting workbook yesterday, and put it to work today—and immediately discovered a problem I hadn't seen until "field testing." It's virtually the same as the problem that I had with the "Next Blank" code, which
Hans solved for me:
That is to say, it "remembers" where it left off. So if, for instance, if I run through the sheet, glance at the duplicates it finds, then go back to fix one I know is not valid, and then try to run through the sheet to double-check, it just says "No more duplicates." Because that's where
it left off.
That's bad enough; I don't trust either myself or my computer and so I double-check things as a matter of practice, and it won't let me do it. But to make matters worse, if I go on to another sheet (where I know there's a duplicate, because I put it there) it still remembers "No more duplicates" and won't find the one on the new sheet. Bear in mind the form has been unloaded in between, because it automatically unloads when I say "Okay" to the "No more duplicates" message.
Here is the code as it stands now. The very first line after the declarations is comparable to the one you altered,
Hans, to fix the problem with the "Next Blank" code—but I can't see the parallel between them well enough to judge how to change it.
Code: Select all
Sub Find_Duplicates()
Static r1 As Long, r2 As Long, m As Long
Dim v As Variant, a As Variant
Dim UFD As Object
Set UFD = UF_Dupes
m = Range("D" & Rows.Count).End(xlUp).Row
a = Range("D1:D" & m).Value
Do
Do
r1 = r1 + 1
If r1 >= m Then
UFD.Hide
MsgBox "No more duplicates", vbInformation, "The End"
GoTo TheEnd ' added 04/30/20~5.07PM
End If
v = a(r1, 1)
Loop Until v <> ""
For r2 = r1 + 1 To m
If a(r2, 1) = a(r1, 1) Then
UFD.TextBox1 = Range("A" & r1).Value
UFD.TextBox2 = Range("C" & r1).Value
UFD.TextBox3 = Range("D" & r1).Value
UFD.TextBox4 = Range("A" & r2).Value
UFD.TextBox5 = Range("C" & r2).Value
UFD.TextBox6 = Range("D" & r2).Value
UFD.TextBox7 = r1
UFD.TextBox8 = r2
Exit Sub
End If
Next r2
Loop
TheEnd: Unload UFD
End Sub
Thank you...