Replace with macro

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
Hello everybody
I have a range ("A1:A100") and some cells have within its text the following pattern
(anytext)
I want to replace that pattern with "" so as to get rid of this pattern
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Don't explain, demonstrate. Show us two or three examples of the input and output. By the time you've done that, I shall have written most the code.
 
Upvote 0
Are you just trying to change cells like this:

123asdf456 to 123456
12abc34def56 to 123456
a1b2c3texttexttext45text6text to 123456

?
 
Upvote 0
the pattern includes ( and also includes )
for example

got you man (154624g gggg)
I want to get rid of (154624g gggg)
 
Upvote 0
But it always has the brackets around it, yes? Please show us some examples.
 
Upvote 0
Try:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]Public Sub ReplaceSomeText()[/FONT]
[FONT=Fixedsys][/FONT] 
[FONT=Fixedsys]  Dim ws As Worksheet
  Dim iRow As Long
  Dim iPosOpen As Long
  Dim iPosClose As Long
  
  Set ws = ThisWorkbook.Sheets(1)
  
  For iRow = 1 To 100
    iPosOpen = InStr(ws.Cells(iRow, "A"), "(")
    iPosClose = InStr(ws.Cells(iRow, "A"), ")")
    If iPosOpen > 0 And iPosClose > 0 Then
      ws.Cells(iRow, "A") = Left(ws.Cells(iRow, "A"), iPosOpen) & Mid(ws.Cells(iRow, "A"), iPosClose)
    End If
  Next iRow
  
End Sub
[/FONT]
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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