DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Excel - pattern matching in cells  (Read 41,932 times)

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Excel - pattern matching in cells
« on: March 16, 2011, 11:19 PM »
Can't believe that there isn't a really simple way of doing this, but then I've been surprised by Excel before…

I have a invoice numbers which all have the format three letters followed by three digits (eg. WHS005).
If the data doesn't match this pattern it should be rejected.

I've Googled, read the help files and failed spectacularly.

Anyone got the answer?

Thanks, Ampa

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Excel - pattern matching in cells
« Reply #1 on: March 17, 2011, 12:10 AM »
You're likely best off looking into VBA and macros. Excel functions are limited and don't really have any good text processing. The lack of regular expression support in Office is quite frankly astounding.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,840
    • Coding Snacks
Re: Excel - pattern matching in cells
« Reply #2 on: March 17, 2011, 12:15 AM »
what is it you want to do, and how are you trying to do it?

is the format of the invoice numbers consistent, ie 3 letters and 3 numbers?

this quick formula will give you a flag

=IF(AND(TYPE(LEFT(A14,3))=2,ISNUMBER(VALUE(RIGHT(A14,3)))),"","XX")

Note that it doesn't properly test the alpha segment, ie WHS and WH2 both evaluate as text in my quick test

Unless you have a consistent naming convention you may need to test each character

and string length may be another test...


Alcohol is photoshop, bottled
Askhole - someone who ask for advice, then ignores it
"Look wise, say nothing, and grunt. Speech was given to conceal thought" - Sir William Osler

moonwatcher

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 8
Re: Excel - pattern matching in cells
« Reply #3 on: March 17, 2011, 07:04 AM »
If you don't mind using UDFs (User Defined Excel Functions), regular expression matching is also possible:

Public Function RegExMatch(ByVal vsStringIn As String, ByVal vsPattern As String) As Boolean
    Dim objRegEx As Object
    Set objRegEx = CreateObject("VBscript.regexp")

    objRegEx.Global = True
    objRegEx.MultiLine = True
    objRegEx.pattern = vsPattern
   
    RegExMatch = objRegEx.Test(vsStringIn)
   
    Set objRegEx = Nothing
End Function
The worksheet formula would then look like this:

=RegExMatch(A1, "[A-Z]{3}\d{3}")
More about UDFs: How to Create Custom User Defined Excel Functions,  Excel Custom Functions using VBA

kfitting

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 593
Re: Excel - pattern matching in cells
« Reply #4 on: March 17, 2011, 10:46 AM »
Basic tutorial on using regular expressions in Excel VBA: http://www.tmehta.com/regexp/index.htm
Regular Expression guide (many languages): http://www.regular-expressions.info/
List of articles showing some other ways of using Regex in Excel VBA: http://excelicious.wordpress.com/category/regular-expressions/

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Excel - pattern matching in cells
« Reply #5 on: March 17, 2011, 11:02 AM »
Basic tutorial on using regular expressions in Excel VBA: http://www.tmehta.com/regexp/index.htm
Regular Expression guide (many languages): http://www.regular-expressions.info/
List of articles showing some other ways of using Regex in Excel VBA: http://excelicious.wordpress.com/category/regular-expressions/

That's some good stuff there!

I'm still hoping that they add in pure .NET at some point as I don't really like VBA.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Ampa

  • I am cute ;)
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 592
Re: Excel - pattern matching in cells
« Reply #6 on: March 17, 2011, 11:22 AM »
The lack of regular expression support in Office is quite frankly astounding.

Glad you agree! I realise that they are available via VB, but why on earth can't I have easy GUI access in dialogues such as Conditional Formatting and Data Validation?!

If you don't mind using UDFs (User Defined Excel Functions), regular expression matching is also possible:

This looks great! Thanks for such a clear complete and yet concise reply!  :up:

Basic tutorial on using regular expressions…

Cheers, useful reading matter.

Target: Thanks for your input too also :)

kfitting

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 593
Re: Excel - pattern matching in cells
« Reply #7 on: March 17, 2011, 12:30 PM »
Actually, what I like less than having "no Regular Expression support" in office, is having "partial support only in Word."  Look at the Word Find help and you'll see some regex-like stuff.  To me it just completely confuses the issue (personal opinion of course). 

At least with VB you can get it back, but yes, it would certainly be nice to have it built in (unless you dont dont trust the probability of MS' implementation being helpful of course!). 

bob99

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 345
Re: Excel - pattern matching in cells
« Reply #8 on: March 17, 2011, 02:24 PM »
In addition to the help & great site links above the forums at Mr. Excel might be of help. http://www.mrexcel.com/forum/index.php
I looked and Mr. Excel was briefly mentioned in the forums back in 09.  The forum members are quite active like they are here and helped me out recently with something I was trying to do.

On the main page http://www.mrexcel.com/ he also has tutorial videos, a blog and other helpful information.  Some free some some not.

Not affiliated with them in any way.


Original address: https://www.donationcoder.com/forum/index.php?topic=26086.0