wild card search

Chas17

Well-known Member
Joined
Oct 16, 2002
Messages
657
a user enters a string in a text box on a form, presses a cmd button to start a search. the string fom the texr box is placed in
Range("SearchInput") example string 2JRCEPV0100E**VALVEX

I then run the sub below to copy all matching entries to a different range.

Sub SearchE()
NN = Range("NextTN").Value 'number of entries in first range copied from

SI = Range("SearchInput")
z = 2

For x = 1 To NN
If Cells(x, 4) = SI Then
For y = 1 To 9
Cells(z, y + 26) = Cells(x, y)
Next y
z = z + 1
End If
Next x

End Sub

Of course only exact matches copy. How would I write it so the user could use a wild card, so they could enter say 2JRCEPV0100E% (if % was the wild card)?

thanks,
Chas
 

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

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
Hi

There are a couple of ways of doing this.

1) If you only put in the first part of the searchstring (say 2JRCEPV0100E) then you could do something like

If left(Cells(x, 4),1,len(SI)) = SI Then where you just match on the first number of characters.

2) Another option is to use the Find facility. This will enable you to use the wildcards. Sample code to use the find
Set c = Range("a1:a7").Find(what:="2jrcepv0100e*")
firstaddr = c.Address
MsgBox c
Set c = Range("a1:a7").FindNext(c)
While c.Address <> firstaddr
MsgBox c
Set c = Range("a1:a7").FindNext(c)

Wend
This will cycle through all the entries that match. You can then manipulate as required.


HTH

Tony
 
Upvote 0

Forum statistics

Threads
1,186,365
Messages
5,957,445
Members
438,306
Latest member
Crystal_Blue

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