Suppress event

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Sheet1 contains two columns of data: Name and Quantity.

I want the user to enter a value in the Name column before a value is entered into the Quantity column.

Quantity contains Data Validation. ensuring only a whole number greater than 0 can be accepted.

This is my code:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 2 Then

        If Me.Cells(Target.Row, 1).Value = vbNullString Then
    
            MsgBox "Enter name first"
        
            Application.EnableEvents = False
        
            Application.Undo
        
            Application.EnableEvents = True
        
        End If

    End If

End Sub

If the user enters a numerical value in column 2 but column 1 does not contain a name, the message box shows up, as expected.

The problem is if the user enters a non-numerical value in column 2 (when again column 1 does not contain a name), DV kicks in, showing invalid value entered.

If I click Cancel, the message box shows up twice.

How can I amend it so the message box only shows up once if the user enters a non-numerical value into column 2?

Thanks
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Why not just do it all with DV using
Excel Formula:
=AND(A2<>"",AND(B2>0,INT(B2)=B2))
and uncheck "Ignore blank"
 
Upvote 0
Solution
Why not just do it all with DV using
Excel Formula:
=AND(A2<>"",AND(B2>0,INT(B2)=B2))
and uncheck "Ignore blank"
Thanks, seems easier with just DV but was puzzled why the event occurred twice.
 
Upvote 0
It's because you changed the cell twice, once to enter an invalid value & then removed it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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