Same String Appears Twice in Text Field

CPGDeveloper

Board Regular
Joined
Oct 8, 2008
Messages
190
Is there a way I can identify records where the same string appears twice in a particular field?

For example, if I have the following:

ID, Field1
1, PO BOX 2
2, PO BOX 2 PO
3, APT 1
4, APT APT 2

How would I pull out rows 2 and 4 in a query?

Thanks!
 

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.
make a module
put this code in it
Code:
'*****************************************
Function has_dup_strings(v As Variant) As Boolean
    
    has_dup_strings = False
    
    Dim counter As Long
    Dim counter2 As Long
    Dim num_elements As Long
    Dim arry As Variant
    Dim string_to_test As Variant
    
    arry = Split(v, " ")
    num_elements = UBound(arry)
    
    For counter = 0 To num_elements
        string_to_test = arry(counter)
        For counter2 = counter + 1 To num_elements
            If string_to_test = arry(counter2) Then
                has_dup_strings = True
                Exit Function
            End If
        Next
    Next
    
End Function
'*****************************************

make a query and use the function in the query
Code:
SELECT 
  Table1.id, 
  Table1.t, 
  has_dup_strings ( [t] ) AS Expr1
FROM 
  Table1;

not thoroughly tested, but seems to work
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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