Using regexp in VBA

JRRyan

Board Regular
Joined
Jul 12, 2010
Messages
55
OK I'm definitely moving well out of my depth here now, but I can't find any significantly useful info online to help me out.

I have a dynamic list of data that includes headings and subheadings. I need to display just the headings on another worksheet and I need to strip the first section of the head off and just display the second half.

The headings are in the following format:

TEAM A1A - Team Name

The word team is obviously the same length for all, but the A1A code can be 2 or 3 characters long, and can be a mix of letters and numbers. The dash is always the same, but the team name could be 2, 3, or 4 worlds long. So, using a function like MID() doesn't work (not easily anyway).

I have been looking at Regular Expressions, and I think using something like rePostMatch could help me to strip out the team name, but I have no idea how to get this to work in a function and frankly I'm getting totally confused between RegExp and regExpr in vbscript. Any help GREATLY appreciated!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You shouldn't need regexp for that - a formula will do it:
=trim(mid(A1,search("-",A1)+1,len(A1)))
 
Upvote 0
Ah, See! Why couldn't I think of that! makes perfect sense.

I'll use that formula, thanks for the assist.

Would still be interested in the proper regexp though, now that I've got into it.
 
Upvote 0
This would be one way:

Code:
Function GetTeam(s As String)
With CreateObject("vbscript.regexp")
    .ignorecase = True
    .Pattern = "TEAM ...? - "
    GetTeam = .Replace(s, "")
End With
End Function
 
Upvote 0
Here is what I had come up with...

Before in Col A, After in B and C:
Excel Workbook
ABC
1TEAM A1A - Team NameTeam NameTeam Name
2TEAM B11C - My TeamMy TeamMy Team
3TEAM C - Your TeamYour TeamYour Team
4TEAM DDDD - My Other TeamMy Other TeamMy Other Team
5TEAM F - You have another team?You have another team?You have another team?
Sheet3
Excel 2003

Rich (BB code):
Sub exa()
Dim MyRange As Range, Cell As Range
    
Dim REX As Object
    
    Set MyRange = Range("A1:A5")
    
    Set REX = CreateObject("VBScript.RegExp")
    With REX
        .Global = False
        .Pattern = ".+?\-"
        
        For Each Cell In MyRange
            If .Test(Cell.Value) Then
                Cell.Offset(, 1).Value = Trim(.Replace(Cell.Value, vbNullString))
            End If
        Next
    End With
    
    For Each Cell In MyRange
        If CBool(InStr(1, Cell.Value, "-")) Then
            Cell.Offset(, 2).Value = Trim(Mid(Cell.Value, InStr(1, Cell.Value, "-") + 1))
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top