Text String Duplicates

G

Gary Thomson

Hi

I have a text string in a cell, which can contain up to 20
characters, come of which may be duplicates

eg:

aaabbbbb
abcdeftttgggg
aaaaaaaaaaaaaaaaa


etc


I want to disaply all letters minus any dupliactes

eg in the examples above

ab
abcdeftg
a


Many Thanks for helping
 
F

Frank Kabel

Hi
one way:
1. insert the following macro in a module of your workbook:
Public Sub find_char_duplicates()
Dim cell As Range
Dim rng As Range
Dim return_str As String
Dim i As Integer
return_str = ""
Set rng = Selection
For Each cell In rng
With cell
If .Value <> "" Then
For i = 1 To Len(.Value)
If InStr(1, return_str, Mid(.Value, i, 1), _
vbTextCompare) = 0 Then
return_str = return_str & Mid(.Value, i, 1)
End If
Next i
.Offset(0, 1) = return_str
return_str = ""
End If
End With
Next
End Sub

2. Highlight the range with your chars and invoke this macro. It will
paste the string without duplicates in the cells right to your
selection
 
B

Bob Chisholm

Gary said:
Hi
I have a text string in a cell, which can contain up to 20
characters, come of which may be duplicates

aaabbbbb
abcdeftttgggg
aaaaaaaaaaaaaaaaa

I want to disaply all letters minus any dupliactes

ab
abcdeftg
a

Many Thanks for helping

The following function returns what you are seeking, and can be asigned
to
wherever you want the result displayed.
==========================================
Private Function UniqueChars(Source As String) As String
Dim iI As Integer
Dim iJ As Integer
Dim iK As Integer
UniqueChars = ""
For iI = 1 To Len(Source)
For iJ = 1 To Len(UniqueChars)
If Mid(Source, iI, 1) = Mid(UniqueChars, iJ, 1) Then
Exit For
End If
Next iJ
If iJ > Len(UniqueChars) Then
UniqueChars = UniqueChars & Mid(Source, iI, 1)
End If
Next iI
End Function
==========================================

This version is case sensitive, and accepts all characters in the source
string.
If it were to be made case-insensitive, one could replace "Mid(Source,"
in the function
code with "Mid(Lcase(Source),"

Hope this helps
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top