Assistance with RegEx Pattern Find and Replace

DBezel

New Member
Joined
May 13, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all.
Seeking some assistance in coding a worksheet to identify a pattern and replace a portion with text.
In column G, I have a list of team names which read like: White, John AR IC Westman Supervisor Team. I am wanting to remove AR which is a variable state abbr and IC for independent contractor and replace both with VM. So result would be White, John VM Westman Supervisor Team. Westman is the name of a contractor and we have about 7 different variable contractor names that I'm prepared to loop in. Last Name, First Name will also be different combinations.

This is what I have so far but it isn't working out. Trying to figure out if this will work somehow or if I will have to loop for each Contractor name.

Sub TestRegEx()
Dim regex As Object, regexMatches As Object
Dim r As Range, rC As Range
Dim sContractors As String, sContractor As String

sVendors = "Contractor1|Contractor2|Contractor3|Contractor4|Contractor5|Contractor6|Contractor7"


Set r = Range("G2", Cells(Rows.Count, "G").End(xlUp))

Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^(\S+, \S+ ).+?( sVendors .+)$"
regex.IgnoreCase = True

For Each rC In r

Set regexMatches = regex.Execute(rC.Value)
If regexMatches.Count > 0 Then
rC.Value = regex.Replace(rC.Value, "$1VM$2")

End If
Next rC

End Sub

Any help appreciated. Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi perhaps
VBA Code:
Sub TestRegEx()
    Dim regex As Object
    Dim r As Range, rC As Range
    Dim sVendors
    sVendors = "Contractor1|Contractor2|Contractor3|Contractor4|Contractor5|Contractor6|Contractor7"
    Set r = Range("G2", Cells(Rows.Count, "G").End(xlUp))
    Set regex = CreateObject("VBScript.RegExp")
    regex.Pattern = sVendors
    regex.IgnoreCase = True
    For Each rC In r
        If regex.Test(rC) Then
            rC.Value = regex.Replace(rC.Value, "VM")
        End If
    Next rC
End Sub
 
Upvote 0
Solution
Try matching the following pattern . . .

VBA Code:
regex.Pattern = "^(.+)(?:AR\sIC)(\s(?:" & sVendors & ").+)$"

Hope this helps!
 
Upvote 0
Appreciate you both! Both helpful in aiding me to understand RegEx patterns. Used a combo of both to come up with the below which worked.


Sub TeamSupervisorUpdate()

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


Dim a, i As Long
Dim r As Range, rC As Range
Dim regex As Object, regexMatches As Object
Dim sVendors As String

sVendors = "Contractor1|Contractor2|Contractor3|Contractor4|Contractor5"

Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^(.+)(?:\S\S\sIC)(\s(?:" & "Contractor1|Contractor2|Contractor3|Contractor4|Contractor5" & ").+)$"

Set r = Range("K2", Cells(Rows.Count, "K").End(xlUp))

For Each rC In r
Set regexMatches = regex.Execute(rC.Value)

If regexMatches.Count > 0 Then
rC.Value = regex.Replace(rC.Value, "$1VM$2")
End If
Next rC

'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
So glad you sorted out
Thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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