Find matching values in one column

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,596
Office Version
  1. 2007
Platform
  1. Windows
I’m looking to find matching values “customers names” in one column.
This column being column A

I can’t use a code where the user would manually enter a customers name as that would take far to long.

The code needs to decipher the names in column A itself.
The code would only need to be interested in customers names that match & have appeared more than once.

The names are saved like so.
TOM JONES 001
BOB SMITH 003

The code would need to ignore the 001 002 003 etc etc part & concentrate on the name.

Maybe best to put these names in a listbox along with its row number ??

What do you think, any advice welcome please.
Thanks.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
1. Are you still using Xl2007. If not, please update to your current version
2. Please supply 8-10 records of data, using XL2BB and then mock up your expected results. This will expedite a solution.
 
Upvote 0
Morning,
I hope this is ok for you.

The code should look in column A for any customers name that appear more than once & populate a listbox to show the user the customers in question.

In this example the code should find the following & populate a listbox

TOM JONES 001 Row 10
TOM JONES 002 Row 18

MEL SMITH 001 Row 14
MEL SMITH 002 Row 19
MEL SMITH 003 Row 29

PAUL MEA 001 Row 12
PAUL MEA 002 Row 23

My complete list at present has say 2450 rows.
Once this has been complete i can the put something in place so i dont have to use this agin.

Many Thanks
Windows 7 Professional
Office 2007 is being used.


TEST.xlsm
ABCDEFGHIJ
802/01/2017DAVID WINTON 001JAGUAR SMART KEYKX469585427GBEBAY04/01/2017ROYAL MAIL
902/01/2017KELVIN GOH 001ORDERRS039542413GBWEB SITE11/01/2017ROYAL MAIL
1002/01/2017TOM JONES 001SUZUKI SMART REMOTEKX469585435GBEBAY04/01/2017ROYAL MAIL
1102/01/2017JAMES GOLBY 001TOUAREG KEY BLADEX 2KX469585413GBEBAY04/01/2017ROYAL MAIL
1203/01/2017PAUL MEA 001FORD KEY143434RS039542427GBEBAY11/01/2017ROYAL MAIL
1303/01/2017JAMES WRATTEN 001SMART CAR KEYKX469585444GBEBAYN/AROYAL MAIL
1403/01/2017MEL SMITH 001STEERING LOCKP9524468EBAY05/01/2017ROYAL MAIL
1504/01/2017BLAKE BURCHILL 001FORD KEY80 BITKX469585475GBEBAY06/01/2017ROYAL MAIL
1604/01/2017MAURICE CARUANA 001HONDA S2000 FOB348RS039542435GBEBAY11/01/2017ROYAL MAIL
1704/01/2017NICOLAE COSTACHESCU 001HONDA S2000 FOB453KX469585458GBEBAY05/01/2017ROYAL MAIL
1805/01/2017TOM JONES 002HONDA S2000 FOB348KX469585489GBEBAY06/01/2017ROYAL MAIL
1905/01/2017MEL SMITH 002ORDERAF567319466GBWEB SITE06/01/2017ROYAL MAIL
2006/01/2017JONAS MALMBERG 001FORD GALAXY KEYID42RS039542696GBEBAY12/04/2017MY HERMES
2106/01/2017JOHN WILLIAMS 003TRANSIT BLUE HORSE SHOEKX469585492GBEBAYN/AMY HERMES
2209/01/2017ALEX CHEERS 001HONDA LOGOREDSTAMPEBAY16/01/2017ROYAL MAIL
2309/01/2017PAUL MEA 002HONDA S2000 FOB350KX469585515GBWEB SITE11/01/2017ROYAL MAIL
2409/01/2017DAVID MCGRATH 001VW LUPO KEYID 44KX469585501GBEBAY10/01/2017ROYAL MAIL
2510/01/2017ROGER MATHEWS 001MAZDA FLIP REMOTEKX469585529GBEBAY11/01/2017ROYAL MAIL
2610/01/2017GRAHAM DANIELS 001SMART CAR REMOTE1BRS039542444GBEBAY16/01/2017ROYAL MAIL
2711/01/2017AUTO CARE SALES 001SUZUKI KEY4CKX469585532GBEBAY12/01/2017ROYAL MAIL
2816/01/2017MARK WALKER 001SPARKRITE FOBKX469585577GBEBAY21/01/2017ROYAL MAIL
2916/01/2017MEL SMITH 003SUZUKI KEY4D65KX469585563GBWEB SITE19/01/2017ROYAL MAIL
POSTAGE
 
Upvote 0
Hi, so I took a stab and made some progress for you possibly:

The code will put the duplicate names and row number into a 2D array "Dup_names()" - which you can use to do anything else with if you like.
My last routine just accesses each item in that array, and puts the list in columns H (name) and I (row number of interest).

Hope it helps you in your quest.

Rob
VBA Code:
Sub duplicates()

Dim x, count, lastrow As Long
Dim arr_names(), dup_names() As String

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ReDim arr_names(1 To lastrow)
ReDim dup_names(1 To 2, 1 To lastrow)

arr_names = Range("B1:B" & lastrow)

For Rnum = 1 To lastrow
    arr_names(Rnum, 1) = Left(arr_names(Rnum, 1), Len(arr_names(Rnum, 1)) - 4)
Next Rnum

x = 1  'set row number start for duplicates array
For Rnum = 1 To lastrow
    check_name = arr_names(Rnum, 1)
    For Lnum = Rnum + 1 To lastrow
        If check_name = arr_names(Lnum, 1) Then
            dup_names(1, x) = check_name
            dup_names(2, x) = Rnum
            x = x + 1
        End If
    Next Lnum
Next Rnum

ReDim Preserve dup_names(1 To 2, 1 To x - 1)

'display duplicates in Col H & I
For Count = 1 To x - 1
    Range("H" & Count) = dup_names(1, Count)
    Range("I" & Count) = dup_names(2, Count)
Next Count

End Sub
 
Upvote 0
Hi,
When i run the code i see variable not defind.
This is shown in yellow

Rich (BB code):
For Rnum = 1 To lastrow
 
Upvote 0
you can add it to the Dim line for Long at the too of the routine ?
its a long integer.

cheers
Rob
 
Upvote 0
Ok so I added Dim Rnum As Integer but the code then fails a few lines down.

Then we have a power cut so typing from phone.
 
Upvote 0
So power on & as mentioned above i have added
Dim Rnum As Interger
But now it shows Variable not defined for this line check_name = arr_names(Rnum, 1)
 
Upvote 0
Book1
ABCD
1CUSTOMERIS ON ROW(S)
2DAVID WINTON8
3KELVIN GOH9
4TOM JONES10, 18
5JAMES GOLBY11
6PAUL MEA12, 23
7JAMES WRATTEN13
8MEL SMITH14, 19, 29
9BLAKE BURCHILL15
10MAURICE CARUANA16
11NICOLAE COSTACHESCU17
12JONAS MALMBERG20
13JOHN WILLIAMS21
14ALEX CHEERS22
15DAVID MCGRATH24
16ROGER MATHEWS25
17GRAHAM DANIELS26
18AUTO CARE SALES27
19MARK WALKER28
20
21
Sheet2

Based on the sheet you've provided, that is what I come up with using this code
VBA Code:
Sub NamesOnRows()
    Dim lr As Long
    Dim rng As Range, cel As Range
    Dim dic As Object
    Dim cust As String

With Sheets("POSTAGE")
    lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    Set rng = .Range("B8:B" & lr)
End With

Set dic = CreateObject("Scripting.Dictionary")

For Each cel In rng
    'just the name
    cust = Trim(Left(cel.Value, Len(cel.Value) - 3))
    'check if name exists in dictionary
    If Not dic.exists(cust) Then
        'if not add it with it's row number
        dic(cust) = cel.Row
    Else
        'if exists, add this row number also
        dic(cust) = dic(cust) & ", " & cel.Row
    End If
Next cel

' Write the customer names and row numbers to another sheet
With Sheets("Sheet2")   ' change to suit
    .Range("B2").Resize(dic.count) = Application.Transpose(dic.keys)  ' the customer names
    .Range("C2").Resize(dic.count) = Application.Transpose(dic.items) ' the rows they're on
End With

End Sub
 
Upvote 0
Hi,

With the code above i have now installed.
The sheet in which the values are stored is Sheet4 & columns are reference is B2 C2

I run the code & it populates the column B with customers name BUT not the rows that they are in.

I then tried it on the file that was supplied in the group doing the same thing & all works fine.
Strange as the file i supplied was from the sheet im trying to use it on

Take a look at the screenshot please

Code in use
VBA Code:
Private Sub MoreThanOne_Click()
    Dim lr As Long
    Dim rng As Range, cel As Range
    Dim dic As Object
    Dim cust As String

With Sheets("POSTAGE")
    lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    Set rng = .Range("B8:B" & lr)
End With

Set dic = CreateObject("Scripting.Dictionary")

For Each cel In rng
    'just the name
    cust = Trim(Left(cel.Value, Len(cel.Value) - 3))
    'check if name exists in dictionary
    If Not dic.Exists(cust) Then
        'if not add it with it's row number
        dic(cust) = cel.Row
    Else
        'if exists, add this row number also
        dic(cust) = dic(cust) & ", " & cel.Row
    End If
Next cel

' Write the customer names and row numbers to another sheet
With Sheets("Sheet4")   ' change to suit
    .Range("B2").Resize(dic.count) = Application.Transpose(dic.Keys)  ' the customer names
    .Range("C2").Resize(dic.count) = Application.Transpose(dic.Items) ' the rows they're on
End With
End Sub
 

Attachments

  • EaseUS_2024_07_13_17_32_44.jpg
    EaseUS_2024_07_13_17_32_44.jpg
    95.2 KB · Views: 15
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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