+ Reply to Thread
Results 1 to 28 of 28

VBA to Prevent Data Entry in Cell with Data Validation List

  1. #1
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    VBA to Prevent Data Entry in Cell with Data Validation List

    Afternoon, all!

    I'm after a little help and guidance from the VBA wizards, please.

    I have the following set-up (this is just a simple example, but properly reflects the real layout):

    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    1
    Pupil Subject
    A
    No. A
    Subject
    Capacity
    2
    1
    Ar
    Ar
    1
    Ar
    10
    3
    2
    Gm
    BS
    0
    BS
    5
    4
    3
    MS
    Ge
    5
    CP
    5
    5
    4
    Ge
    Gm
    1
    ClasCi
    5
    6
    5
    Ge
    Hi
    0
    DT
    5
    7
    6
    Ge
    MS
    1
    Dr
    5
    8
    7
    Ge
    SS
    0
    FT
    5
    9
    8
    Ge
    Fr
    5
    10
    9
    GPE
    5
    11
    10
    Ge
    5
    12
    Gm
    5
    13
    Hi
    5
    14
    La
    5
    15
    MS
    5
    16
    Mu
    5
    17
    RE
    5
    18
    SS
    5
    19
    Sp
    5
    Sheet: Sheet1

    Column B contains subject choices with drop-down list selection.

    F2:G8 are conditionally formatted to turn red when capacity is reached for that subject (see the capacity grid far right).

    Once capacity is reached in column B, if someone tries to change any existing entries to that subject OR add another entry of that subject (e.g. in B10), I would like a pop-up message ("Subject full - choose a different option") and for the user to be stopped from entering the subject in question into that column.

    In other words, there cannot be more that 5 instances of Ge in the subject column.

    However, if someone wishes to change any existing Ge to something else, the drop-down still needs to work, so I don't want the cell locking.

    Is this something that is easy to do with VBA?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,912

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Something like this?

    Please Login or Register  to view this content.
    Rory

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Thanks, Rory. I am about to try this. I will let you know how I get on.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    OK - so I have probably done something wrong, as it isn't doing anything.

    Where and how should I add the code? I tried right-clicking Sheet1 in the VBA editor and pasting the code - no luck. Then I did the same for This Workbook - still nothing. What have I not done correctly, please?

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    If it's not to lock but just not allow to change then you could probably add a warning when defining the dropdown list which will only offer the options retry or cancel but it will still allow to lower the value entered
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    You can use this "Custom" formula in with Data Validation in B2:B10: =(COUNTIF($B$2:$B2, $B2)<6)
    In the Data Validation "Error Alert" tab you can enter Subject full - choose a different option.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    I did it in the red cell, the max is linked to the value in the J column

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Quote Originally Posted by Keebellah View Post
    If it's not to lock but just not allow to change then you could probably add a warning when defining the dropdown list which will only offer the options retry or cancel but it will still allow to lower the value entered
    I don't understand what you are suggesting. The drop-down entry is not a numeric value.
    Last edited by AliGW; 03-26-2019 at 11:58 AM.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Quote Originally Posted by Keebellah View Post
    I did it in the red cell, the max is linked to the value in the J column
    Thanks, but that's not what I am looking for. The data entry is in column B - that's where I need the VBA to work. I can't use data validation as these cells already have a data validation list. I do not just want a message, either - I want to stop the data entry.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Quote Originally Posted by Mumps1 View Post
    You can use this "Custom" formula in with Data Validation in B2:B10: =(COUNTIF($B$2:$B2, $B2)<6)
    In the Data Validation "Error Alert" tab you can enter Subject full - choose a different option.
    No, the cells in question already have data validation (a drop-down list).

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    If you can tell me what I've done wrong with Rory's VBA, I'd be grateful.

  12. #12
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,912

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    You had put the code into a normal module, not the sheet module.

  13. #13
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    The code goes in the worksheet's module but I don't know what it's supposed to do

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Rory - thanks. I knew it would be a silly error. I'll try again!

  15. #15
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Noticed it throws an error when you clear a cell with a value
    Add the red line of code to the code
    Please Login or Register  to view this content.

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    That seems to be working fine now. Thanks again, Rory!

    Noticed it throws an error when you clear a cell with a value
    It's not throwing any errors here, but thanks for the advice.

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,912

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Quote Originally Posted by AliGW View Post
    It's not throwing any errors here, but thanks for the advice.
    I actually tweaked the version in your amended file when I uploaded it as I noticed the problem!

  18. #18
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Okay, it only happens if you enter a value and later decide to remove it.

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Ah, Rory - that explains it!

    Sorry to be a dimwit here, but can someone just talk me through how to get the code in the right place. I have it working in Rory's sample file, but when I try to apply it to the real file, it does nothing.

    It looks as though it is in exactly the right place.

    I cannot share the real file.

  20. #20
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,912

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Right click the relevant worksheet tab, choose view code, then paste it in.

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    It's OK - I made a moronic mistake - I forgot to change the range from column B to column D. It works perfectly - thank you!

  22. #22
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Sorry - one more question. In the real sheet I have four columns for which I need to do the same check.

    I have amended the code like this:

    Please Login or Register  to view this content.
    Am I right to change the Sub names? Is this the correct way to do it? To be clear, I need each of the four monitored ranges to work independently of each other, but they use the same lookup matrix.

    Thanks again!

  23. #23
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    My 2 cents worth:
    Please Login or Register  to view this content.

  24. #24
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Are you able to give a quick explanation of this line, please?

    Please Login or Register  to view this content.

  25. #25
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    It replaces the range represented by the variable "MonitorRange". As well, Excel doesn't allow more than one Worksheet_Change event so they have to be combined.

  26. #26
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    I see - thank you. I will give this a try.

  27. #27
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,799

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    Thank you again - this seems fine now.

  28. #28
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA to Prevent Data Entry in Cell with Data Validation List

    You are very welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2016, 08:27 PM
  2. Prevent Data Entry using Data Validation
    By HangMan in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-24-2015, 03:01 PM
  3. [SOLVED] Force data entry in cell with drop down list (i.e. Prevent user from leaving cell blank)
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-15-2014, 05:28 AM
  4. [SOLVED] Prevent duplicating data when using data validation list
    By amr7 in forum Excel General
    Replies: 2
    Last Post: 02-10-2014, 06:23 PM
  5. Replies: 4
    Last Post: 12-19-2013, 10:44 AM
  6. Replies: 0
    Last Post: 10-06-2011, 07:03 AM
  7. Data validation to prevent duplicate entry.
    By vishu in forum Excel General
    Replies: 0
    Last Post: 03-14-2005, 08:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1