Avoid duplicates in a listbox populated from multiple ranges using search keyword

sivakumar123

New Member
Joined
Jul 26, 2012
Messages
19
Hi,

I have a listbox which is populated from multiple ranges by using keyword search. I want to avoid duplicates in the search result.
Listbox name : Members
Ranges : teamRng and nameRng
Following is the code i use :

Code:
Private Sub searchBox_Change()
Dim cellName As Range, nameRng As Range, firstName As String
Dim cellTeam As Range, teamRng As Range, firstTeam As String, myCell As String

ContactForm.Members.Clear
Set [B]nameRng [/B]= ContactWorkBook.ActiveSheet.Range("myName")
Set [B]teamRng [/B]= ContactWorkBook.ActiveSheet.Range("teamName")

Set cellName = nameRng.Find(What:=searchBox.Value & "*", After:=nameRng(nameRng.Count), LookIn:=xlValues, _
                            Lookat:=xlWhole, SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, MatchCase:=False)
                            
Set cellTeam = teamRng.Find(What:=searchBox.Value & "*", After:=teamRng(teamRng.Count), LookIn:=xlValues, _
                            Lookat:=xlWhole, SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, MatchCase:=False)

[I]'To populate Member Names[/I]
If Not cellName Is Nothing Then
   firstName = cellName.Address
   Do
     With Members
     .AddItem cellName
     .List(.ListCount - 1, 1) = cellName
     If searchBox.Value = "" Then
        .Clear
     End If
     End With
   Set cellName = nameRng.FindNext(After:=cellName)
   Loop While cellName.Address <> firstName
End If


[I]'To populate Team Names[/I]
[COLOR=#ff0000]Dim sFound As Boolean, i As Integer
sFound = False
If Not cellTeam Is Nothing Then
   For i = 0 To Members.ListCount - 1
      If cellTeam = Members.List(i) Then
         sFound = True
      End If
   Next
   If Not sFound Then
      firstTeam = cellTeam.Address
      Do
        myCell = cellTeam.Value
        With Members
          .AddItem cellTeam
          .List(.ListCount - 1, 1) = cellTeam
          If searchBox.Value = "" Then
          .Clear
          End If
        End With
      Set cellTeam = teamRng.FindNext(After:=cellTeam)
      MsgBox myCell
      Loop While cellTeam.Address <> firstTeam And cellTeam <> myCell
   End If
End If
[/COLOR]
Call sortList
End Sub

The code in red color is a result of lot of tweaking. ;)
This is where I was failed to remove duplicates. It worked partially though.

Kindly help me out on removing duplicates from the listbox.:)

Best Regards
Sivakumar
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,215,762
Messages
6,126,744
Members
449,335
Latest member
Tanne

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