Calseon
Calseon

Reputation: 361

C# Excel: Find cells using pattern matching

Is there a way to use the Range.Find() method with numbers-only wildcards or regex?

I'm trying to find cells with values such as "2015 M05", "2016 M08", "2017 M01", to parse the dates in a spreadsheet.

enter image description here

So far the best I can think of is using the wildcard "?" to form the search string "???? M??"

object missingVal = System.Reflection.Missing.Value;
Excel.Range match = allCells.Find("???? M??", missingVal, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, missingVal, missingVal);

However, that will run into problems as soon as I have a two-worded label somewhere having the second word starting with "M".

Edit: I think the simplest thing is if I had some sort of digit-only wildcard that I could use in Range.Find(). However "#" doesn't seem to work so I was wondering if there was some workaround?

Upvotes: 0

Views: 1787

Answers (1)

UndeadBob
UndeadBob

Reputation: 1129

You can use the * in the pattern. Something like *?M?? assuming the M## pattern is unique.

Upvotes: 2

Related Questions