[Webinar] Streamline your web hosting managementRegister Today


Regex Expression with Excel VBA

Posted on 2015-01-09
Medium Priority
Last Modified: 2015-01-11
How to modify the regex expression in order to handle an additional case, -93ASV-0.59 and break it down into 3 parts:
(1) -93
(2) ASV
(3) -0.59
    Set regex = CreateObject("VBScript.RegExp")
    regex.Pattern = "(\d+)([A-Z]+)(\d+.\d+)"
    If regex.test(theString) Then
        Set matches = regex.Execute(theString)
        arr(0) = matches.Item(0).SubMatches(0)
        arr(1) = matches.Item(0).SubMatches(1)
        arr(2) = matches.Item(0).SubMatches(2)
        getPart = arr
    End If

    arr(0) = 11
    arr(1) = AFD
    arr(2) = 7.69
Question by:AXISHK
  • 3
  • 2
  • 2
LVL 85

Accepted Solution

ozo earned 1200 total points
ID: 40541679

Author Comment

ID: 40541832
Is there any reference for Regex expression for VBA ? Is the syntax is the similar as other language ?

Author Comment

ID: 40541858
What's mean for -?, no need to add \ in front of it ?


Is there nay reference on the regex usage ? I can't find the meaning for "-"  ? Tks
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 48

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 800 total points
ID: 40543423
A dash has no special meaning in Regex so does not need to be escaped with the back-slash.

"-?" is looking for zero or more dashes so that the pattern -?\d+ will match -93 or 93.
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 40543428
A good article on Regex in VBA, written by Excel genius Patrick Matthews, can be found here.
LVL 85

Expert Comment

ID: 40543437
"-?" is looking for zero or one dashes
"-*" would look for zero or more dashes
"-+" would look for one or more dashes

A dash can have special meaning inside [],  e.g. "[A-Z]"

Author Closing Comment

ID: 40543550

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Manually copying shapes and their assigned macros one by one to a new location can be tedious, but if you use the Excel utility workbook attached to this article, the process will be much quicker and easier.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question