简体   繁体   中英

How to search for items with multiple “-” in excel or VBA?

I have a list of item numbers (100K) like this:

在此输入图像描述

Some of the items have format like SAG571A-244-4 (thousands) which need to be filtered so I can delete them and only keep the items that have ONE hyphen per SKU. How can I isolate the items that have two instances of "-" in it's SKU? I'm open to solutions within Excel or using VBA as well.

Native text filters don't seem to be capable of this. I'm stumped.

As per John Coleman's comment, "*-*-*" can be used to isolate strings that have at least two dashes in them.

I would add that if you're entering them as a custom text filter, you should lose the double quotes (so just *-*-* ) as otherwise the field seems to interpret the quotes literally.

Seems to work for me.

产量

If you want just an excel formula to verify this and give you a result of the number of hyphens (0, 1, or 2+), here is one:

=IF(ISERROR(SEARCH("-",A1)),"0",IF(ISERROR(SEARCH("-",A1,IFERROR(SEARCH("-",A1)+1,LEN(A1)))),"1","2+"))

Replace A1 with your relevant column, then fill down. This is kind of a terrible way to do this performance wise, but you avoid using VBA and possibly xlsm files.

The code first checks to see if there is one hyphen, then if there is it checks to see if there is another hyphen after the position the first one was found. Looking for multiple hyphens in this manner is cumbersome and I don't recommend it.

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