Display an alert if data already exists

c3rn_nz

New Member
Joined
Apr 4, 2019
Messages
4
Hi everyone, I require some help creating an alert please.

The situation is as follows.
- I have a workbook called Invoices
- There are two sheets in the workbook, one called Current and the other called Processed
- Users enter in pertinent details daily and then a certain user processes the data and moves (cut/paste) the entries from the Current sheet to the one called Processed.
- In column B, users enter an invoice number

I would like to create an alert for when a user enters an invoice number in any row of column B and that invoice number already exists in column B across either of the two sheets.

Ideally a pop up would be displayed, just providing a warning. The user can then choose to close the warning/pop up and continue if they wish.

Any assistance in this matter would be appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
In the worksheet module for the worksheet 'Processed', paste the following code and you should be fine...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Set TRng = Target.Range("A1")
    If TRng.Column <> 2 Then Exit Sub
    TVal = TRng.Value
    CountValP = WorksheetFunction.CountIf(Range("B:B"), TVal) - 1 '-1 to exclude just entered value
    CountValC = WorksheetFunction.CountIf(Worksheets("Current").Range("B:B"), TVal)
    AlertTxt = ""
    If CountValP > 0 Then AlertTxt = AlertTxt & "This worksheet already contains " & CountValP & IIf(CountValP = 1, "entry", "entries") & vbCrLf
    If CountValC > 0 Then AlertTxt = AlertTxt & "Worksheet 'Current' contains " & CountValC & IIf(CountValC = 1, "entry", "entries")
    If Len(AlertTxt) > 0 Then AlertTxt = "Invoice no." & TVal & vbCrLf & AlertTxt: MsgBox (AlertTxt)
End Sub
 
Upvote 0
similar but different

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Msg As String, C  As Boolean, P As Boolean
    If Target.CountLarge = 1 And Target.Column = 2 Then
        If Target = "" Then Exit Sub
        C = WorksheetFunction.CountIf(Target.EntireColumn, Target) > 1
        P = WorksheetFunction.CountIf(Sheets("Processed").Range("B:B"), Target) > 0
        If C Then Msg = Target & " elsewhere in this sheet"
        If P Then Msg = Msg & vbCr & Target & " also in Processed"
        If C Or P Then MsgBox Msg, vbExclamation, "Alert!"
    End If
End Sub
 
Upvote 0
I should have said that the above goes in sheet Current..
 
Upvote 0
I should have said that the above goes in sheet Current..

In that case, paste the following code in the VBA module for sheet 'Current'

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Set TRng = Target.Range("A1")
    If TRng.Column <> 2 Then Exit Sub
    TVal = TRng.Value
    CountValC = WorksheetFunction.CountIf(Range("B:B"), TVal) - 1 '-1 to exclude just entered value
    CountValP = WorksheetFunction.CountIf(Worksheets("Processed").Range("B:B"), TVal)
    AlertTxt = ""
    If CountValC > 0 Then AlertTxt = AlertTxt & "This worksheet already contains " & CountValC & IIf(CountValC = 1, "entry", "entries") & vbCrLf
    If CountValP > 0 Then AlertTxt = AlertTxt & "Worksheet 'Processed' contains " & CountValP & IIf(CountValP = 1, "entry", "entries")
    If Len(AlertTxt) > 0 Then AlertTxt = "Invoice no." & TVal & vbCrLf & AlertTxt: MsgBox (AlertTxt)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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