Another Code Please!

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I need a code that when I select a column if a word i.e ABC or a number i.e 123 appears anywhere within a cell more than once I would like all duplicates removed just leaving the one.

e.g

Cell A1 Before

ABC 123 123 ABC ABC 456 DEF

Cell A1 After

ABC 123 456 DEF

and so on down every row until the last.

I will need to use this on more than one column so its best if the code works on the active column.

Thanks.
 
Hi Rick, just to state what I have found by trying it on dazwm's original request and on his "2AD-FTV 2AD-FHV 2AD-FTV 2AD-FHV 2AD-FTV 2AD-FHV" posting is it is taking out all the instances of the first part of the string i.e. in
Code:
Const RemoveMe As String = "ABC,123" 'NOTE - no spaces around the commas

The 123 had 1 instance but the ABC was totally removed. I reversed the string and this time ABC remained but 123 was removed.

I got a different result to dazwm in the last post, I got 2AD-FHV.

No trouble with the hyphen but again the first part of the string was removed.

Hope this is of some help
 
Upvote 0

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.
Rick, Sorry due to my slow typing my last statement referred to your first code, can see no problem when testing the 2nd version but haven't tested for case sensitivity
HTH
 
Upvote 0
Rick, Sorry due to my slow typing my last statement referred to your first code, can see no problem when testing the 2nd version but haven't tested for case sensitivity
HTH

I am glad you can get it to work!!
 
Upvote 0
dazwm, Ricks code is looking for duplicates of ABC and 123. If you asking does it look for random duplicates in the string (which I guessing you are) then no it doesn't
 
Upvote 0
dazwm, Ricks code is looking for duplicates of ABC and 123. If you asking does it look for random duplicates in the string (which I guessing you are) then no it doesn't

Yes I am. The duplicates could be anything. Basically anything seperated by a space if it is duplicated I want only one left. It could be like any of the following

ABC123
2AD-FHV
118.98
M118.988
4G13

and so on....
 
Upvote 0
I got it to work now but a small problem. Some of the data has dashes and it doesnt do it correct.

e.g

<TABLE style="WIDTH: 218pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=290><COLGROUP><COL style="WIDTH: 218pt; mso-width-source: userset; mso-width-alt: 10605" width=290><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 218pt; HEIGHT: 12.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl64 height=17 width=290>2AD-FTV 2AD-FHV 2AD-FTV 2AD-FHV 2AD-FTV 2AD-FHV

becomes
<TABLE style="WIDTH: 218pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=290><COLGROUP><COL style="WIDTH: 218pt; mso-width-source: userset; mso-width-alt: 10605" width=290><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 218pt; HEIGHT: 12.75pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" class=xl66 height=17 width=290>2AD FTV FHV</TD></TR></TBODY></TABLE>

instead of 2AD-FTV 2AD-FHV


</TD></TR></TBODY></TABLE>

Change the pattern from
.Pattern = "\b\w+\b"

to
.Pattern = "\b\w+-?\w+\b"

To use in VBA instead of formula try this (select the entire column and run the macro below)

Code:
Sub useNoDups()
    Dim rng As Range, aCell As Range
    
    With Selection
        Set rng = Range(.Cells(1, 1), .Cells(.Cells(Rows.Count, 1).End(xlUp).Row, 1))
    End With
    
    For Each aCell In rng
        aCell = noDups(aCell.Text)
    Next aCell
    
End Sub

HTH

M.
 
Upvote 0
Change the pattern from
.Pattern = "\b\w+\b"

to
.Pattern = "\b\w+-?\w+\b"

To use in VBA instead of formula try this (select the entire column and run the macro below)

Code:
Sub useNoDups()
    Dim rng As Range, aCell As Range
 
    With Selection
        Set rng = Range(.Cells(1, 1), .Cells(.Cells(Rows.Count, 1).End(xlUp).Row, 1))
    End With
 
    For Each aCell In rng
        aCell = noDups(aCell.Text)
    Next aCell
 
End Sub

HTH

M.

Thanks Marcelo, that works but I need the pattern adjusted to cater for a . (dot) amongst data as well as a dash.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,711
Members
452,939
Latest member
WCrawford

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