Searching using TextBox and ListBox

campbell1093

New Member
Joined
Aug 27, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
New poster, so I apologize if this has been answered elsewhere.

I've been working on a tracker for work, where I have 10 columns and (right now) 35 rows. It's been an ongoing project for several months, and I've gone through several iterations.

In my workbook, I have a search userform and an entry userform. The entry form works perfectly, except I'm trying to get it to detect the date for when the event occurs and select the correct month tab. The search function has worked flawlessly up until my last update and allows me to search all data from all tabs, but my last update didn't change any of the code pertaining to the search feature. Now, I get a "run time error '13' type mismatch" whenever I enter a single character into my search userform.

I'm about to my wits end with this, as I've tried copying the working code from the previous version, I've tried importing the whole userform as is, and I've tried commenting out some other code I've since added.

For reference, I use the following code:

Excel Formula:
Private Sub TextBox1_Change()

Set sh = Sheets("Data")
Dim i As Long
Dim x As Long
Dim p As Long

Me.ListBox1.Clear

Me.ListBox1.AddItem "Case"
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = "Complainant"
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = "Offense"
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = "Level"
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = "Reported"
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = "Suspect"
Me.ListBox1.List(ListBox1.ListCount - 1, 6) = "Disposition"
Me.ListBox1.List(ListBox1.ListCount - 1, 7) = "Assigned"
Me.ListBox1.List(ListBox1.ListCount - 1, 8) = "Action"
Me.ListBox1.List(ListBox1.ListCount - 1, 9) = "As Of"
SearchData.ListBox1.ColumnWidths = "50;90;120;30;45;120;80;50;85;50"
'width "case;victim;offense;level;reported;suspect;disposition;assignment date;actions;as of date"

Me.ListBox1.Selected(0) = True

For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row
[COLOR=rgb(250, 197, 28)]For x = 1 To Len(sh.Cells(i, 2))[/COLOR]
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 1), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Or _
    LCase(Mid(sh.Cells(i, 2), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Or _
    LCase(Mid(sh.Cells(i, 6), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
    With Me.ListBox1
    .AddItem sh.Cells(i, 1)
    .List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 2)
    .List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 3)
    .List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 4)
    .List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 5)
    .List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 6)
    .List(ListBox1.ListCount - 1, 6) = sh.Cells(i, 7)
    .List(ListBox1.ListCount - 1, 7) = sh.Cells(i, 8)
    .List(ListBox1.ListCount - 1, 8) = sh.Cells(i, 9)
    .List(ListBox1.ListCount - 1, 9) = sh.Cells(i, 10)
    End With
End If

Next x
Next i

End Sub


The error in debug will highlight "For x = 1 To Len(sh.Cells(i, 2))", but again, all my previous versions with the same code have not errored.

Attached are the links for the versions I've been working with. Any help is greatly appreciated!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,223,868
Messages
6,175,084
Members
452,611
Latest member
bls2024

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