VBA Code to remove a string of text from cells

Hovecat

New Member
Joined
Oct 24, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I want to develop a VBA script to remove any text in a cell starting from "(". The following shows a before and after image of what I'm trying to explain. Hoping someone can help.

ROMANTIC WARRIOR (IRE)ROMANTIC WARRIOR
ZAAKI (GB)ZAAKI
MR BRIGHTSIDE (NZ)MR BRIGHTSIDE
ALLIGATOR BLOODALLIGATOR BLOOD
GOLD TRIP (FR)GOLD TRIP
MY OBERON (IRE)MY OBERON
PINSTRIPEDPINSTRIPED
FANGIRLFANGIRL
DUAISDUAIS
VICTORIA ROAD (IRE)VICTORIA ROAD
MILITARIZE (NZ)MILITARIZE
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Do you really need VBA ??
A formula solution
Excel Formula:
=IF(ISERROR(LEFT(A1,FIND(" (",A1)-1)),A1,LEFT(A1,FIND(" (",A1)-1))
 
Upvote 0
Do you really need VBA ??
A formula solution
Excel Formula:
=IF(ISERROR(LEFT(A1,FIND(" (",A1)-1)),A1,LEFT(A1,FIND(" (",A1)-1))
Thanks Michael. Your formula works and I'm happy to use it. But I want to be able to execute it on a regular basis with different data sets. I thought it would be better to create a macro.
 
Upvote 0
Try
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
    If InStr(Range("A" & r).Value, " (") Then
    Range("B" & r).Value = Left(Range("A" & r).Value, InStr(Range("A" & r).Value, " (") - 1)
    Else:
     Range("B" & r).Value = Range("A" & r).Value
    End If
Next r
End Sub
 
Upvote 0
Try
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
    If InStr(Range("A" & r).Value, " (") Then
    Range("B" & r).Value = Left(Range("A" & r).Value, InStr(Range("A" & r).Value, " (") - 1)
    Else:
     Range("B" & r).Value = Range("A" & r).Value
    End If
Next r
End Sub
That's brilliant! Thanks so much.
 
Upvote 0
If your opening parenthesis, when present, is always preceded by a space and located at the end of the text (as in your examples), then this one-liner macro would work...
VBA Code:
Sub RemoveFromSpaceParenToEnd()
  Columns("A").Replace " (*", ""
End Sub
 
Last edited:
Upvote 0
If your opening parenthesis, when present, is always preceded by a space and located at the end of the text (as in your examples), then this one-liner macro would work...
VBA Code:
Sub RemoveFromSpaceParenToEnd()
  Columns("A").Replace " (*", ""
End Sub
Yes that's work well too. Thanks Rick.
 
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