简体   繁体   中英

Alternative to wildcard searching for multiple criteria; vba-excel

I have a spreadsheet with ~50k rows; one of the columns has a description. I want to filter the description column using multiple wildcards. That is apparently a gamebreaker in Excel. I used a code similar to the following and the end result was individually sorting per each criteria:

Sub Product_Sort()

ActiveSheet.Range("$A$1:$J$47853").AutoFilter Field:=10, Criteria1:= _
Array("=*ProductA*", "=*ProductB*", "=*ProductC*", "=*ProductD*"), Operator:=xlOr

End Sub

In the end, there are 87 wildcard criteria that need to be searched.

I think I've thought of an alternative... to use the existing sort filters, and for each search in the array to add an "x" to the End(xlToRight) +1 column. The problem is I do not know enough about loops to make that work.

Is anyone able to help me sort out this mess? Additionally, if there is another alternative to this process, it would be appreciated for you to enlighten me.


Edit 20150825, 08:21CDT

The data I'm working with have large strings which may contain the term to match., eg1 The issue with ProductA is the widget, eg2 ProductC has pants, or eg3 Someone threw out ProductF. There are ~50k rows with entries like this in my data.

In the above examples, and based on the initial code, I am searching only for ProductA, ProductB, ProductC, and ProductD; this would give a result matching the wildcard for eg1 and eg2 only, with possibly #N/A or #Value for eg3.

In trying sums, matches, and vlookups, I cannot seem to find a way for the individual terms to be the wildcards (eg ProductC) in my array. I have tried a VLookUp similar to this: =vlookup(" " & Sheet1! A$1:A$38 & " ", J2,0,False) which... honestly gave me inconsistent information. I had hits in places they shouldn't have been.

Hopefully that adds some more info to the issue.

I tried using http://www.excelguru.ca/forums/showthread.php?270-vLookup-with-wildcards-%28equivalent-of-quot-contains-quot-%29-best-solution and can't seem to get the sum/iferror string to pan out for me.

Put all your wildcards in a column on another sheet and use this formula:

=LARGE(IFERROR(MATCH("*"&Sheet2!A$1:A$4&"*",A2,0),0),1)

Confirm as an array formula with ctrl+shift+enter. 1 means its a match, 0 means it did not match.

Sheet2!A$1:A$4 is the range containing your wildcards, and A2 is the current cell with the string you want to check. You can autofill down to the rest of your column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM