Getting .Find Method to stop at bottom of range (NOT loop around)

AskMyDog

New Member
Joined
Nov 13, 2015
Messages
16
Hi all,

Thanks for all of the help previously.

Here's a problem I'm having: I am using the .find method in a Do...While Loop to find duplicate values, but I want the loop to stop when it gets to the bottom of the range, not wrap around and search from the top (another way to put it would be to have it find every instance below the original value, not above). I've tried the following code, but its not looping and finding subsequent duplicates (i.e. it just finding the 2nd duplicate when it enters the Do...Until Loop, but looping back to find the 3rd, 4th, duplicates, etc).

Here's the code:

Sub test()

Dim g As Long
Dim lastRow1 As Long
Dim arr As Variant
Dim h As Long, i As Variant, j As Long
Dim arr1 As Variant
Dim dic As Object
Dim dic2 As Object
Dim Key As String
Dim First_Dupl As Variant
Dim Next_Dupl As Variant
Dim Last_Row As Long
Dim Table3 As ListObject
Dim table_top As Long
Dim Type_Col As Long


Set dic = CreateObject("Scripting.Dictionary")
Set dic2 = CreateObject("Scripting.Dictionary")
dic.RemoveAll
dic2.RemoveAll
Set Table3 = Worksheets("Reconcile Meds Here").ListObjects("Table3")
Table3.Range.ClearFormats
Last_Row = Table3.DataBodyRange.Rows.Count
table_top = Table3.Range.Row


With Worksheets("Reconcile Meds Here").ListObjects("Table3").ListColumns("Medication Type")
arr = .DataBodyRange
For g = 1 To UBound(arr)
If Not IsEmpty(arr(g, 1)) Then
arr1 = Split(arr(g, 1), ", ")
For h = LBound(arr1) To UBound(arr1)
Key = LCase(arr1(h))
dic(Key) = dic(Key) + 1
Next
End If
Next
For Each i In dic
If dic(i) > 1 Then
Set First_Dupl = .Range.Find(i)
j = First_Dupl.Row
With Table3.ListRows(First_Dupl.Row - table_top).Range.Interior
.ColorIndex = 22
.TintAndShade = 0
End With
Do
Set Next_Dupl = .Range.Find(i, after:=First_Dupl)
With Table3.ListRows(Next_Dupl.Row - table_top).Range.Interior
.ColorIndex = 22
.TintAndShade = -0.15
End With
Set First_Dupl = .Range.FindNext
Loop While Not IsNull(First_Dupl) And First_Dupl.Row > j
End If
Next
End With
End Sub

Thanks everyone!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

You have two Find statements inside the Do…Loop and depending on the nature of your data it might be possible that the wraparound occurs after Next_Dupl.

Please have a look at the below code (modified from yours) and see if it helps.

Code:
[FONT=Consolas][SIZE=2][COLOR=Navy]Do
   Set Next_Dupl = .Range.Find(i, after:=First_Dupl)
   [B]If j = Next_Dupl.Row Or IsNull(Next_Dupl) Then Exit Do[/B]
   With Table3.ListRows(Next_Dupl.Row - table_top).Range.Interior
      .ColorIndex = 22
      .TintAndShade = -0.15
   End With
   Set First_Dupl = .Range.FindNext
   [B]If j = First_Dupl.Row Or IsNull(First_Dupl) Then Exit Do[/B]
Loop[/COLOR][/SIZE][/FONT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top