+ Reply to Thread
Results 1 to 5 of 5

RegEX VBA Split Method

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    RegEX VBA Split Method

    Hi all,


    Using Excel 2013.

    Does VBA RegEx Engine have a split method?
    I have not been able to find any documentation on it.

    What would the pattern(s) be to split string like:
    adsf@#$23.45
    ghjkGHJ%&*45.8

    So I end up with
    adsf@#$ | 23.45
    ghjkGHJ%&* | 45.8

    Or will I need to pass the string to the RegEx function twice with 2 different patterns?

    thx
    w
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: RegEX VBA Split Method

    I'm not aware of any native VBA regexing, but maybe they added it in 2013, I don't know of a split method of the vbscript regex either. You could do something like the below:
    Function RegSplit(myString As String) As Variant
    
    Static regex As Object
    Dim g, l
    If regex Is Nothing Then Set regex = CreateObject("vbscript.regexp")
    
    Set regex = CreateObject("vbscript.regexp")
    With regex
        .Pattern = "[0-9\.]*$"
      Set l = .Execute(myString)
    End With
    
    g = Split(myString, l(0))
    g(1) = l(0)
    
    RegSplit = g
    
    End Function
    Last edited by Kyle123; 09-02-2013 at 12:21 PM.

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: RegEX VBA Split Method

    Try this:
    Function SplitLastNums(MyText As String) As String
    Dim iPos As Integer
    Dim arrRegEx
    
    Dim regex As Object
    Set regex = CreateObject("vbscript.regexp")
    
    With regex
        .Pattern = "\d"
        Set arrRegEx = .Execute(MyText)
    
        If arrRegEx.Count > 0 Then
            iPos = arrRegEx(0).firstindex + 1
            SplitLastNums = Left(MyText, iPos - 1) & " | " & Right(MyText, Len(MyText) - iPos + 1)
        Else
            SplitLastNums = MyText
        End If
    End With
    End Function
    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: RegEX VBA Split Method

    function M_regexless(c00)
       M_regexless=split(replace(replace(c00,"$","|"),"*","|"),"|")
    
    '   M_regexless=split(replace(replace(c00,"$","|"),"*","|"),"|")(0)
    '   M_regexless=split(replace(replace(c00,"$","|"),"*","|"),"|")(1)
    end function



  5. #5
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: RegEX VBA Split Method

    Thanks all.

    Thanks Ron,

    The function woks great!

    Thx
    w

+ 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. [SOLVED] How to REGex with VBA words
    By Odeen in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 12-03-2012, 09:12 AM
  2. RegEx Problem
    By Marshall80 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-24-2011, 04:49 AM
  3. Replies: 1
    Last Post: 11-20-2005, 07:50 PM
  4. Split() method
    By reddog9069 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-20-2005, 12:16 PM
  5. [SOLVED] RegEx to parse something like this...
    By R Avery in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2005, 07:06 PM

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