Listbox Search only working with numbers

banshee001

New Member
Joined
Sep 28, 2022
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I have a search function that im using to list items in the listbox based on whats entered in the textbox, but as of now its only working with numbers.
the problem i had was that it wont search part of words or number, such as if the number was "12345" and i entered "345" in the textbox it wont list it. But for words/terms/characters im getting an error "type mismatch".
I tried different approaches but it wont search for parts of the words (using .textlength previously instead of .text)

the code is as follows:

VBA Code:
Sub search_all()
userform1.textbox1=lcase(userform1.textbox1)
dim sh as worksheet
set sh=sheet1
dim i as long
dim x as long
dim p as long, k as integer

with userform1.listbox1
userform1.listbox1.rowsource=""
.columnhead=false
.columncount=30
.columnwidth="........................"
.list=sh.range("A1", "AE1").value

for i = 2 to sh.range("B" & Rows.Count).end(xlup).row
for x = 1 to len(sh.cells(i,1))
for k = 1 to 30
p=userform1.textbox1.text
if Lcase(mid(sh.cells(i,k),x,p)) like "*" & userform1.textbox1.text & "*" then
.add item
.list (userform1.listbox1.listcount -1,0)=sh.cells(i,1)
..............
...........
.....
End if
next k
next x
next i
end with
end sub

Also i have another problem which is i get duplicates of the items when i search, for example if i search "345" i get three results of the same item.
thanks in advance
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,215,445
Messages
6,124,894
Members
449,194
Latest member
JayEggleton

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