Delete duplicate text in cell

JimH

Board Regular
Joined
Apr 21, 2002
Messages
94
Office Version
  1. 2010
Platform
  1. Windows
I have a spreadsheet that has duplicate sentences in all the cells in Column B, and I would like an automated way to delete the duplicate text. For example, cell B1 contains the following text: *See spot run. *See spot run.

I'm thinking a macro could be written to find the first asterisk then the second and then delete everything after the second asterisk. This process would be repeated on every cell in the column.

There may be a better way, but this is all I could come up with.

Everyone's help would be greatly appreciated.

Thank you.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:
Code:
Option Explicit

Sub BetweenStars()
Dim RNG As Range
Dim Cell As Range

Set RNG = Range("B:B").SpecialCells(xlConstants)

    For Each Cell In RNG
        If InStrRev(Cell, "*") > 1 Then _
            Cell = Left(Cell, InStrRev(Cell, "*"))
    Next Cell

End Sub
 
Upvote 0
Thanks, Jerry. It works great.

What would you add to the code to make it delete the asterisks after deleting the duplicate text?
 
Upvote 0
Hi JimH,

I'm no good with macros but the following formula might help as long as all duplicates follow the same format (will remove all asterisks, then just paste values if required)

=MID(B1,2,SEARCH("~*",B1,2)-3)

Cheers,
alx7000
 
Upvote 0
What would you add to the code to make it delete the asterisks after deleting the duplicate text?

Maybe this:
Code:
Option Explicit

Sub BetweenStars()
Dim RNG As Range
Dim Cell As Range

Set RNG = Range("B:B").SpecialCells(xlConstants)

    For Each Cell In RNG
        If InStrRev(Cell, "*") > 1 Then _
            Cell = Trim(Replace(Left(Cell, InStrRev(Cell, "*")), "*", ""))
    Next Cell

End Sub
 
Upvote 0
Hi

If the second asterisc is always preceeded by a space, like in the example, you can also use Edit->Replace to delete the extra text. This would work in any spreadsheet that you receive without having to use macros or formulas.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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