VBA Question: Do not pop up message box when deleting data in cell

agonysWeeper

Board Regular
Joined
Feb 4, 2011
Messages
146
Hello,

Looking for some help on this vba code from somewhere in google, I am almost no knowledge on VBA so I'm trying to do some additional logic.
so when the contract number has existing record on the 'J' column, the message will pop up and click OK, after clicking the OK button and trying to delete the contract number on that cell (using keyboard delete), the message will pop up again, so I need not to have the pop up when deleting the number.

The below code run as well in other column and I just want it to run in column J.


Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Row = 1 Then Exit Sub             ' IF ITS A HEADER, DO NOTHING.
    
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    
    Dim myDataRng As Range
    Dim cell As Range
     
    ' WE WILL SET THE RANGE (SECOND COLUMN).
    Set myDataRng = Range("J1:J" & Cells(Rows.Count, "J").End(xlUp).Row)
     
    For Each cell In myDataRng
        cell.Offset(0, 0).Font.Color = vbBlack          ' DEFAULT COLOR.
    
        ' LOCATE DUPLICATE VALUE(S) IN THE SPECIFIED.
        If Application.Evaluate("COUNTIF(" & myDataRng.Address & "," & cell.Address & ")") > 1 Then
        MsgBox "This contract number has existing record above, please just update the existing record and Open the status - If this is alt request due to different term, or need to have separate request, please proceed."
        Exit Sub
        'cell.Offset(0, 0).Font.Color = vbRed        ' CHANGE COLOR TO RED.
        End If
    Next cell
     
    Set myDataRng = Nothing
ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
.
The macro runs as desired here. I cannot replicate the double msgbox notices.

???
 
Upvote 0
Something weird in my file, it works in a plain new worksheet, but in my file keeps on doing wrong.
For example, there is the 3rd times the contract number was provided and trying to delete the 3rd one.
 
Last edited:
Upvote 0
.
You may need to rebuild the workbook from scratch into a new workbook.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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