Multiple lines with the same data? How do i stop this from happening?

jayjay2022

New Member
Joined
May 28, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello eveyrone,

Everytime timw I use the search textbox and don't have caps on, the listbox shows multiple lines with the same data. My code does use the Ucase command but I would like to use upper or lower case with no duplicates

How do i stop this from happening?

My code

-----------------------------------------------
Private Sub textBox10_Change()

Dim sh As Worksheet
Set sh = Sheets("Daily Jobs")
Dim i As Long
Dim X As Long
Dim p As Long
Me.ListBox10.Clear

Me.TextBox10 = Format(StrConv(Me.TextBox10, vbUpperCase))

For i = 2 To sh.Range("B" & Rows.Count).End(xlUp).Row
For X = 1 To Len(sh.Cells(i, 2))
p = Me.TextBox10.TextLength

If UCase(Mid(sh.Cells(i, 2), X, p)) = Me.TextBox10 And Me.TextBox10 <> "" Then

With Me.ListBox10
.AddItem sh.Cells(i, 2)
.List(ListBox10.ListCount - 1, 0) = sh.Cells(i, 1)
.List(ListBox10.ListCount - 1, 1) = sh.Cells(i, 2)
.List(ListBox10.ListCount - 1, 2) = sh.Cells(i, 3)
.List(ListBox10.ListCount - 1, 3) = sh.Cells(i, 4)
.List(ListBox10.ListCount - 1, 4) = sh.Cells(i, 6)
.List(ListBox10.ListCount - 1, 5) = sh.Cells(i, 5)
.List(ListBox10.ListCount - 1, 6) = sh.Cells(i, 7)
.List(ListBox10.ListCount - 1, 7) = sh.Cells(i, 8)

End With
End If

Next X
Next i

End Sub
------------------------------------------------

Please screen shot attached

Thanks in advance.
 

Attachments

  • UCASE  search code.png
    UCASE search code.png
    12.3 KB · Views: 8

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello Jayjay2022, welcome.
It's hard to explain why this happens.
I can only say textbox "Change" event is little bit tricky.
Try in this way and I hope you will see the difference.
VBA Code:
Private Sub textBox10_Change()

    Dim sh As Worksheet
    Set sh = Sheets("Daily Jobs")
    Dim i As Long
    Dim X As Long
    Dim p As Long
    Me.ListBox10.Clear

    a = sh.Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To a
        For X = 1 To Len(sh.Cells(i, 2))
            p = Me.TextBox10.TextLength
            If UCase(Mid(sh.Cells(i, 2), X, p)) = Me.TextBox10 And Me.TextBox10 <> "" Then
                With Me.ListBox10
                    .AddItem sh.Cells(i, 2)
                    .List(ListBox10.ListCount - 1, 0) = sh.Cells(i, 1)
                    .List(ListBox10.ListCount - 1, 1) = sh.Cells(i, 2)
                    .List(ListBox10.ListCount - 1, 2) = sh.Cells(i, 3)
                    .List(ListBox10.ListCount - 1, 3) = sh.Cells(i, 4)
                    .List(ListBox10.ListCount - 1, 4) = sh.Cells(i, 6)
                    .List(ListBox10.ListCount - 1, 5) = sh.Cells(i, 5)
                    .List(ListBox10.ListCount - 1, 6) = sh.Cells(i, 7)
                    .List(ListBox10.ListCount - 1, 7) = sh.Cells(i, 8)
                End With
            End If
        Next X
    Next i
    Me.TextBox10 = Format(StrConv(Me.TextBox10, vbUpperCase))
   
End Sub
 
Upvote 0
Solution
Hello Jayjay2022, welcome.
It's hard to explain why this happens.
I can only say textbox "Change" event is little bit tricky.
Try in this way and I hope you will see the difference.
VBA Code:
Private Sub textBox10_Change()

    Dim sh As Worksheet
    Set sh = Sheets("Daily Jobs")
    Dim i As Long
    Dim X As Long
    Dim p As Long
    Me.ListBox10.Clear

    a = sh.Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To a
        For X = 1 To Len(sh.Cells(i, 2))
            p = Me.TextBox10.TextLength
            If UCase(Mid(sh.Cells(i, 2), X, p)) = Me.TextBox10 And Me.TextBox10 <> "" Then
                With Me.ListBox10
                    .AddItem sh.Cells(i, 2)
                    .List(ListBox10.ListCount - 1, 0) = sh.Cells(i, 1)
                    .List(ListBox10.ListCount - 1, 1) = sh.Cells(i, 2)
                    .List(ListBox10.ListCount - 1, 2) = sh.Cells(i, 3)
                    .List(ListBox10.ListCount - 1, 3) = sh.Cells(i, 4)
                    .List(ListBox10.ListCount - 1, 4) = sh.Cells(i, 6)
                    .List(ListBox10.ListCount - 1, 5) = sh.Cells(i, 5)
                    .List(ListBox10.ListCount - 1, 6) = sh.Cells(i, 7)
                    .List(ListBox10.ListCount - 1, 7) = sh.Cells(i, 8)
                End With
            End If
        Next X
    Next i
    Me.TextBox10 = Format(StrConv(Me.TextBox10, vbUpperCase))
  
End Sub
Hi EXCEL MAX.

I used the code you provided and it works flawlessly.

I have been using VBA over a month now and the most frustration issue I have are dates and formatting dates.

I truly appricate your help.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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