data validation through Worksheet Change Event

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet with hundreds of lines in col A.

When I ADD a NEW line below, the value in the Range("A" & newline) must match either of the values apples/bananas/oranges.

Is there a way to do this using the Worksheet Change Event, instead of using data validation ?

Thanks.
 

Attachments

  • apples.png
    apples.png
    16.4 KB · Views: 16

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Is this what you mean? It removes any entries that do not match A2:A4

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  
  Set Changed = Intersect(Target, Columns("A"), Rows("5:" & Rows.Count))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If Not IsNumeric(Application.Match(c.Value, Range("A2:A4"), 0)) Then c.ClearContents
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Solution
Thanks Peter

your solution is valuable, however I would prefer something like:

- if it's a NEW row below the existing ones, then the new entry should match the values in an array ("apples","bananas","oranges")

so I need to determine the last used row
and then check the new entry.
 
Upvote 0
I'm not following.
  1. Can you give an example where my code doesn't do what you want.
  2. Are you saying the list to check against ("apples","bananas","oranges") is unrelated to what is above? That is, if A2:A4 contained "plums", "cherries" & "apricots" then A5 would still have to be one of "apples","bananas","oranges"?
 
Upvote 0
Thanks Peter. Let me see if I can explain this a little better.

Set Changed = Intersect(Target, Columns("A"), Rows("5:" & Rows.Count))

here Changed should refer to the new Row.

Are you saying the list to check against ("apples","bananas","oranges") is unrelated to what is above? That is, if A2:A4 contained "plums", "cherries" & "apricots" then A5 would still have to be one of "apples","bananas","oranges"?

YES. A2:A4 are old entries. The check only applies to new entries.
 
Upvote 0
Set Changed = Intersect(Target, Columns("A"), Rows("5:" & Rows.Count))

here Changed should refer to the new Row.
So what happens if
  1. the user enters something in a row below the first empty one? Don't check that entry?
  2. the user enters (or pastes) multiple values in to the next row and other rows immediately after that? Only check the first row of the multiple rows entered?
  3. the user replaces an entry (or multiple entries) that already exists above that "new row"? Don't check such entries?
  4. the user enters "APPLES" instead of "apples" etc?
 
Upvote 0
it's getting too complicated for me I guess... I'll stick to the less complicated and built in Data Validation.

Thanks anyway for your help.
 
Upvote 0
I'll stick to the less complicated and built in Data Validation.
Up to you, but presumably the Data Validation will be in multiple rows, not just the next empty one. :)

Also just pointing out that both DV and using vba have possible drawbacks
Data Validation can be defeated if data is copy/pasted into cells that contain DV
vba can be defeated if the user does not have macros enabled
 
Upvote 0
Thanks Peter. Your initial suggestion deserves to be marked as a possible solution. Which I did just now.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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