I'm trying to use regex in excel VBA to match a pattern within all cells in a column range, and remove the matched patterns to a new column range.
E.g.
- Happy Day Care Club (1124734)
- French Pattiserie (8985D)
- The King's Pantry (G6666642742D)
- Big Shoe (China) Ltd (ZZ454)
Essentially I want to remove the last bracketed portion of each string and transpose this part (without the brackets) into a different column range.
The regex I have so far is "(([^)]+))\z" (which I don't know if this is actually correct), and embedded within this VBA:
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim Myrange As Range
Sheets("Sheet 1").Activate
Range("FF65536").End(xlUp).Select
LastCell = ActiveCell.Address
Set Myrange = ActiveSheet.Range("FF2:" & LastCell)
For Each C In Myrange
strPattern = "(\(([^\)]+)\)\z)"
If strPattern <> "" Then
strInput = C.Value
strReplace = "$1"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
Range("FF2").Select = regEx.Replace(strInput, "$1")
Range("DX2").Select = regEx.Replace(strInput, "$2")
End If
End If
Next
I'm a newbie so please forgive glaringly obvious mistakes.
Many thanks,
\s*(\((.*?)\))$regex if there are(or)inside the parentheses.