Removing "Duplicate Rows" Listbox

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,

Why is it everytime i use the search textbox ,the listbox then comes up with multiple line with the same data?is there a way of preventing it?

VBA Code:
Private Sub TextBox1_Change()
Me.TextBox1 = Format(StrConv(Me.TextBox1, vbLowerCase))
Dim sh As Worksheet
Set sh = Sheets("Sheet1")
Dim i As Long
Dim x As Long
Dim p As Long
Me.ListBox1.Clear
Me.ListBox1.AddItem "Product ID"
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = "ARTG ID"
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = "Product Description"
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = "Status"
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = "Impact Start Date"
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = "Impact End Date"
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = "Usage End Date"


For i = 2 To sh.Range("F" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 6))
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1
.AddItem sh.Cells(i, 2)

'.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
'.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 5)
.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 6)
.List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 7)
.List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 8)
.List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 9)
.List(ListBox1.ListCount - 1, 6) = sh.Cells(i, 10)
'.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
'.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
'.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
'.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
'.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
'.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
'.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
'.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)



End With

End If
Next x
Next i

End Sub
 

Attachments

  • duplicate.jpg
    duplicate.jpg
    120.4 KB · Views: 23

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
If Me.TextBox1 = "" Then Exit Sub
For i = 2 To Sh.Range("F" & Rows.Count).End(xlUp).Row
   If InStr(1, Sh.Cells(i, 6), Me.TextBox1, vbTextCompare) > 0 Then
      With Me.ListBox1
      .additem Sh.Cells(i, 2)
      
      '.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
      '.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
      .List(ListBox1.ListCount - 1, 1) = Sh.Cells(i, 5)
      .List(ListBox1.ListCount - 1, 2) = Sh.Cells(i, 6)
      .List(ListBox1.ListCount - 1, 3) = Sh.Cells(i, 7)
      .List(ListBox1.ListCount - 1, 4) = Sh.Cells(i, 8)
      .List(ListBox1.ListCount - 1, 5) = Sh.Cells(i, 9)
      .List(ListBox1.ListCount - 1, 6) = Sh.Cells(i, 10)
      '.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
      '.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
      '.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
      '.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
      '.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
      '.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
      '.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
      '.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)
      
      
      
      End With

   End If
Next i
 
Upvote 0
Solution
How about
VBA Code:
If Me.TextBox1 = "" Then Exit Sub
For i = 2 To Sh.Range("F" & Rows.Count).End(xlUp).Row
   If InStr(1, Sh.Cells(i, 6), Me.TextBox1, vbTextCompare) > 0 Then
      With Me.ListBox1
      .additem Sh.Cells(i, 2)
    
      '.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
      '.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
      .List(ListBox1.ListCount - 1, 1) = Sh.Cells(i, 5)
      .List(ListBox1.ListCount - 1, 2) = Sh.Cells(i, 6)
      .List(ListBox1.ListCount - 1, 3) = Sh.Cells(i, 7)
      .List(ListBox1.ListCount - 1, 4) = Sh.Cells(i, 8)
      .List(ListBox1.ListCount - 1, 5) = Sh.Cells(i, 9)
      .List(ListBox1.ListCount - 1, 6) = Sh.Cells(i, 10)
      '.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
      '.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
      '.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
      '.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
      '.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
      '.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
      '.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
      '.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)
    
    
    
      End With

   End If
Next i
hey duff my bad it is working and not showing any duplicates anymore.
 
Upvote 0
The code I posted replaces all of this
VBA Code:
For i = 2 To sh.Range("F" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 6))
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1
.AddItem sh.Cells(i, 2)

'.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
'.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 5)
.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 6)
.List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 7)
.List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 8)
.List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 9)
.List(ListBox1.ListCount - 1, 6) = sh.Cells(i, 10)
'.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
'.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
'.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
'.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
'.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
'.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
'.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
'.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)



End With

End If
Next x
Next i
 
Upvote 0
The code I posted replaces all of this
VBA Code:
For i = 2 To sh.Range("F" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 6))
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1
.AddItem sh.Cells(i, 2)

'.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
'.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 5)
.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 6)
.List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 7)
.List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 8)
.List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 9)
.List(ListBox1.ListCount - 1, 6) = sh.Cells(i, 10)
'.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
'.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
'.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
'.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
'.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
'.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
'.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
'.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)



End With

End If
Next x
Next i
Yeah haha i noticed the other line after i hit reply.apologies.haha.thank you again fluff.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
The code I posted replaces all of this
VBA Code:
For i = 2 To sh.Range("F" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 6))
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1
.AddItem sh.Cells(i, 2)

'.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)
'.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)
.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 5)
.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 6)
.List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 7)
.List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 8)
.List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 9)
.List(ListBox1.ListCount - 1, 6) = sh.Cells(i, 10)
'.List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 11)
'.List(ListBox1.ListCount - 1, 10) = sh.Cells(i, 12)
'.List(ListBox1.ListCount - 1, 11) = sh.Cells(i, 13)
'.List(ListBox1.ListCount - 1, 12) = sh.Cells(i, 14)
'.List(ListBox1.ListCount - 1, 13) = sh.Cells(i, 15)
'.List(ListBox1.ListCount - 1, 14) = sh.Cells(i, 16)
'.List(ListBox1.ListCount - 1, 15) = sh.Cells(i, 17)
'.List(ListBox1.ListCount - 1, 16) = sh.Cells(i, 18)



End With

End If
Next x
Next i
fluff with the same code how do i expand my Search option and not just under one Column(F)?
VBA Code:
If Me.TextBox1 = "" Then Exit Sub
For i = 2 To sh.Range("F" & Rows.Count).End(xlUp).Row
   If InStr(1, sh.Cells(i, 6), Me.TextBox1, vbTextCompare) > 0 Then
      With Me.ListBox1
      .AddItem sh.Cells(i, 2)
 
Upvote 0
How many columns do you want to search & what are they?
 
Upvote 0
Ok, how about
VBA Code:
   If InStr(1, Sh.Cells(i, 2), Me.TextBox1, vbTextCompare) > 0 Or InStr(1, Sh.Cells(i, 5), Me.TextBox1, vbTextCompare) > 0 Or InStr(1, Sh.Cells(i, 6), Me.TextBox1, vbTextCompare) > 0 Or InStr(1, Sh.Cells(i, 7), Me.TextBox1, vbTextCompare) > 0 Then
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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