VBA Regex Pattern

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, can someone tell me the pattern for extracting variable length of letters starting from an alphanumeric string, The string will always start with letters first.

Here is the Code I am Using

Code:
Private Sub Test()
    Dim Rng As Range, Fnd As Range, x As Range
    Dim strPattern As String, strInput As String
    Dim RegEx As Object
    Dim Arr As Variant
    Dim i As Integer
    
    Arr = Array("References", "Reference", "Ref's", "Refs", "Ref")
        For i = LBound(Arr) To UBound(Arr)
            Set Fnd = ActiveSheet.Columns.Find(What:=Arr(i), LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        If Not Fnd Is Nothing Then
            Set Rng = Range(Fnd.Offset(1), Cells(Rows.Count, Fnd.Column).End(xlUp))
        End If
    Next i
    
    Set RegEx = CreateObject("VBScript.RegExp")
    For Each x In Rng
    
        'strPattern =  <---------

        If strPattern <> "" Then
            strInput = x.Value

    With RegEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = True
                .Pattern = strPattern
            End With

            If RegEx.Test(strInput) Then
                x.Offset(0, 7) = RegEx.Replace(strInput, "$1")
            Else
                x.Offset(0, 7) = "(Not matched)"
            End If
        End If
    Next x
End Sub
 
By the way, str is not a good name to use for a variable as it is also the name of a built-in VBA function.

Hi Rick I didn't realise that, Thanks for the Info and your help.

I'm also wondering about your use of 'Selection'. It is rare to need to select a range in vba to work with it and selecting can slow your code considerably.

Hi Peter, the reason I am using selection is because the data varies in every workbook and each line in each book varies so some lines won't need extracting the last word, I do a lot of data editing to create a modified versions of workbooks for my company to use.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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