I have a list of data where I have to delete some information in parenthesis, but some should stay.
It looks like that (in separate cells):
A |
---|
Aqua (Water) (100%) |
Parfum (Fragrance) (90%) |
Some Plant (Latinname) Extract (76%) |
And I need to have:
A |
---|
Aqua (100%) |
Parfum (90%) |
Some Plant Extract (76%) |
I used such code in vba:
Cells.Replace What:=" ([A-Z]*)", replacement:="", lookat:=xlPart
but it doesn't work.
Using
Cells.Replace What:=" (*)", replacement:="", lookat:=xlPart
will delete all data in parenthesis. The case seems easy, but I cannot find the solution. I tried also:
Cells.Replace What:=" ({A-Z]*[a-z])", replacement:="", lookat:=xlPart
or:
Cells.Replace What:=" ([A-Z]*[! %])", replacement:="", lookat:=xlPart
But it also didn't work.
EDIT
The case is a little bit complicated. Unfortunately, the data pattern may vary. It is not always "WORD (WORD_TO_DELETE) (PERCENTAGE)" but it is like:
A |
---|
Name1 (10%) |
Name2 (Data_to_delete) RestOfName2 (15%) |
Name3 (Data_to_delete) RestOfName3 (20%), Name4 (Another_data_to_delete) RestOfName4 (25%) |
So I used (of course temporarily) that:
For i = 1 To 3
Sheets("Sheet1").Cells(i, 1).Replace What:=" (A*)", replacement:="", lookat:=xlPart
Next i
For i = 1 To 3
Sheets("Sheet1").Cells(i, 1).Replace What:=" (B*)", replacement:="", lookat:=xlPart
Next i
etc.
And it works perfectly, but look very ugly. I think the regular expressions may be a solution, but I need a little more time to check it.
I know, the solution is to change the structure of the source database and change the string data to some nicer format, but unfortunately I don't have a permission to modify that.
EDIT2
Case closed. RegEx is the solution. The cell A3 is "Lameria Borea (Latinname) Extract (76%), Aqua (Water) (<45%)". The code below will do the job:
Sub test()
Dim i As Integer
Dim strPattern As String: strPattern = " \([A-Z][a-z]*\)"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range
Set Myrange = ActiveSheet.Range("A3")
If strPattern <> "" Then
strInput = Myrange.Value
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
MsgBox (regEx.Replace(strInput, strReplace))
Else
MsgBox ("Not matched")
End If
End If
End Sub
It needs to be a little bit polished to fulfil my goal, but it will resolve all the cases I mention in the first edit.