Audible alarm when Duplicate Condition Met

Rick__

New Member
Joined
Mar 18, 2016
Messages
20
Hi all,

I was wondering if anyone could help me with my Excel question, I've tried to search for the answer and have seen a couple posts but still unclear how to actually do this, and I'm still new to VBA.

I have a list of part numbers in column A, and in column B, I would like to scan in new part numbers, but when a duplicate is found (comparing A column to B column) I would like a sound / alarm to go off to prompt the user because they won't be necessary looking at the screen when scanning in these part numbers.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Right click the sheet name, select "View Code" and paste in the following:

Code:
Private Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Sub Worksheet_Change(ByVal Target As Range)

Dim checkRange As Range
Dim foundRow As Variant
Dim checkCell As Range

' Any cells in column B changed?
Set checkRange = Application.Intersect(Target, Range("B:B"))

' Nothing in column B - we're done
If checkRange Is Nothing Then Exit Sub

' Look at all changed cells
For Each checkCell In checkRange
    ' Is this value already in column A?
    foundRow = Application.Match(checkCell.Value, Range("A:A"), 0)
    If Not IsError(foundRow) Then
        ' Found it - play a sound
        sndPlaySound32 "C:\Windows\Media\chimes.wav", 0&
        
        ' (Optional) - highlight the cell in error in some way
        checkCell.Font.Color = vbRed
    End If
Next checkCell

End Sub

That checks for entries in column B that already occur in column A and plays a sound if it finds one. Change the location of the WAV file to suit.

WBD
 
Upvote 0
It works perfectly, thank you for your help with this. After looking at your code I doubt I would have been able to do this on my own.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,719
Members
452,995
Latest member
isldboy

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