Find duplicate beds in list

arjan kooger

New Member
Joined
Feb 18, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I have a bed list with names in column B and bed numbers in column L ( like 10T, 10B, 11T, 11B etc.).

i use a macro that works pretty well, but it shows only the first names in a message box (in Another sheet) if there are duplicates.

Can anyone help me with a macro what will show both names in the same bed in a message box?

Thanks in advance!

A B L
1Adamson (N)17T
2Aquilina (N)16T
3Bacic (N)34T
4Buruiana (N)11T
5Butter50B
6Ciparis57B
7Devic (N)09T
8Gray11T
9Halliday (N)51B
10Hoeksema (N)10T
11Hoekstra40T
12Jozic13T
13Kristkalns41B
14Martin13T
15Mifsud56B
16Mitrofanovs34T
17Muscat09T
18Pauli12T
19Penava42B
20Rancans17T
21Rocha16T
22Sinenkij (N)13T
23Srac (N)15T

Sub Test()

ThisWorkbook.Sheets("POB").Activate

Set R = Range("L4", Range("L" & Rows.Count).End(xlUp))

For Each c In R

D = Cells(c.Row, c.Column - 10).Value

If WorksheetFunction.CountIf(R, c) > 1 Then If InStr(1, s, c) = 0 Then s = s & c & "," & " - " & D & vbCr

Next

ThisWorkbook.Sheets("All").Activate

MsgBox IIf(s <> "", "Duplicate beds in the List" & vbLf & Mid(s, 1), "No duplicates")

End Sub
 

Attachments

  • MsgBox duplicates.PNG
    MsgBox duplicates.PNG
    7.8 KB · Views: 24

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try such code:
VBA Code:
Sub Test()
Dim R As Variant, dict As Object, key As Variant, i As Long, s As String
Set dict = CreateObject("Scripting.Dictionary")
With ActiveWorkbook.Sheets("POB")
  R = .Range("B4", .Range("L" & .Rows.Count).End(xlUp)).Value
End With
For i = 1 To UBound(R)
  key = R(i, 11)
  If dict.exists(key) Then
    dict(key) = dict(key) & ", " & R(i, 1)
  Else
    dict(key) = R(i, 1)
  End If
Next i
For Each key In dict
  If InStr(dict(key), ", ") > 0 Then s = s & dict(key) & " - " & key & vbCr
Next key
' ActiveWorkbook.Sheets("All").Activate
MsgBox IIf(s <> "", "Duplicate beds in the List:" & vbLf & s, "No duplicates")
End Sub
PS. By using CODE tags, the readability of the code on forum is significantly improved
 
Upvote 0
Solution
Thank you Kaper, this is what i was looking for!!!

I did alter the code a bit coz since in the original list there are spaces and group names it did show those group names as wel.

I Changed start row to B14 and change
VBA Code:
If InStr(dict(key), ", ") > 1
in "1" seems to solve the problem.

Thanks Again!! Problem solved!


VBA Code:
Dim R As Variant, dict As Object, key As Variant, i As Long, s As String
Set dict = CreateObject("Scripting.Dictionary")
With ActiveWorkbook.Sheets("POB")
  R = .Range("B14", .Range("L" & .Rows.Count).End(xlUp)).Value
End With
For i = 1 To UBound(R)
  key = R(i, 11)
  If dict.exists(key) Then
    dict(key) = dict(key) & ", and  " & R(i, 1)
  Else
    dict(key) = R(i, 1)
  End If
Next i
For Each key In dict
  If InStr(dict(key), ", ") > 1 Then s = s & dict(key) & "  -  " & key & vbCr
Next key
ActiveWorkbook.Sheets("All").Activate
MsgBox IIf(s <> "", "Duplicate beds in the List:" & vbCrLf & vbLf & s, "No duplicates")
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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