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: 19

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
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

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
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

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 
Upvote 0

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
How many columns do you want to search & what are they?
 
Upvote 0

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
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,186,808
Messages
5,959,928
Members
438,454
Latest member
leopedrini

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
Top