+ Reply to Thread
Results 1 to 14 of 14

Help with VBA Regular Expression

  1. #1
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Help with VBA Regular Expression

    Hi Forum,

    I've spent hours on trying to get this pattern right but no luck.
    I thought I was following the rules but maybe someone can help here.

    I want to create a regular expression for the below number - NOTE ALL numbers can change to other numbers.

    Please Login or Register  to view this content.
    I've come up with this but it's not working.

    [\d-][0-9][0-9-][0-9][0-9][0-9-][0-9][0-9][0-9][0-9][0-9-][0-9][0-9][0-9]$"
    Remember you are unique, like everyone else

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help with VBA Regular Expression

    Hello noboffinme,

    This macro will display each parsed number in the string. This will work for varying whole numbers provided the number of hyphens remains the same.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    Thanks Leith,
    I can see the result is displaying each piece of the 1-12-123-12345-1234.
    Can you advise how I can get just a match or non match for the whole value of 1-12-123-12345-1234?
    I should mention that these blocks of numbers will stay the same & also the hyphens, it's just the numbers that may change to other numbers from 0-9.
    Appreciate your help.

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Help with VBA Regular Expression

    Quote Originally Posted by noboffinme View Post
    I can see the result is displaying each piece of the 1-12-123-12345-1234.
    Is your text in cells? If so...

    What cells?

    Are there more than one of these numbers per cell?

    Where did you want the outputted values to go to... if there is only one per cell? if there are more than one per cell?

  5. #5
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    Hi Rick,
    The values are in one Cell each in a worksheet.
    I have a list of values, some numbers, some text in a column and I want to loop through the column & find the values that suit the REGEXP pattern.
    So as the loop goes down the column, it will find matches & non matches.
    Thanks

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Help with VBA Regular Expression

    Quote Originally Posted by noboffinme View Post
    Hi Rick,
    The values are in one Cell each in a worksheet.
    One value per cell, but does that mean the value is in a cell by itself or is it in a cell mixed in with other text?

    Also, what output are you looking for (the value itself, the word "Match", something else) and where did you want that output to go to?

  7. #7
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    What I mean by one value is '1-12-123-12345-1234', this is just one long number with hyphens joining them in one cell.
    The ONLY values I have in my file are numbers & hyphens in the format I've supplied ie; 1-12-123-12345-1234. If I come across a value that doesn't suit that format, I want to disregard it & go to the next value.
    I'm going to do some other processing after the matching value is found so an If statement can be done to callout when there's a match, I can take that 1-12-123-12345-1234 value & apply it elsewhere.
    Hope that makes sense.
    Thanks

  8. #8
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Help with VBA Regular Expression

    Quote Originally Posted by noboffinme View Post

    I've come up with this but it's not working.

    [\d-][0-9][0-9-][0-9][0-9][0-9-][0-9][0-9][0-9][0-9][0-9-][0-9][0-9][0-9]$"
    Maybe this is what you want?
    Please Login or Register  to view this content.
    there's a good site if you want to test a regex:
    https://regexr.com/

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Help with VBA Regular Expression

    I think this non-RegExp macro might be faster than a RegExp macro. This code assumes your data is in Column A and put the word "Match" in Column B ext to any number matching your pattern.
    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Help with VBA Regular Expression

    Quote Originally Posted by noboffinme View Post
    What I mean by one value is '1-12-123-12345-1234', this is just one long number with hyphens joining them in one cell.
    The ONLY values I have in my file are numbers & hyphens in the format I've supplied ie; 1-12-123-12345-1234.
    See if this is what you wanted.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    Thanks Rick,
    I've used your answer in conjunction with Leiths as I needed this to be done with a REGEX finction.
    Thanks for your help!

  12. #12
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    Thanks Leith,
    I've used your answer in conjunction with Ricks as I needed this to be an output in one piece.
    Thanks for your help!

  13. #13
    Forum Contributor noboffinme's Avatar
    Join Date
    08-29-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2003/7/10/13/16/19
    Posts
    1,071

    Re: Help with VBA Regular Expression

    Yes thank you for your help jindon!

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Help with VBA Regular Expression

    You are welcome and thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Regular Expression Help
    By wwconslt in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-18-2017, 10:33 PM
  2. [SOLVED] Regular expression
    By chief_abound in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-10-2015, 10:59 PM
  3. [SOLVED] Regular Expression pattern
    By BS Singh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2014, 10:53 PM
  4. Backreferencing In VBA Regular expression
    By BS Singh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2014, 11:46 PM
  5. Regular expression
    By Gourav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2014, 03:23 PM
  6. regular expression for password
    By Gourav in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2014, 04:05 PM
  7. [SOLVED] Regular Expression
    By Kvramana in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 05:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1