+ Reply to Thread
Results 1 to 22 of 22

VBA to Prevent Data Entry in Cell with Data Validation List - Two Criteria Validation

Hybrid View

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

    VBA to Prevent Data Entry in Cell with Data Validation List - Two Criteria Validation

    I got some great help with this bit of code a while back:


    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("D16:G180")) Is Nothing Then Exit Sub
        Dim cell As Range
        For Each cell In Range(Cells(16, Target.Column), Cells(180, Target.Column)) 'Match the range extremes
            If Len(cell.Value) <> 0 Then
                Dim MaxCount As Long
                MaxCount = Range("X6:X25").Cells(Application.Match(cell.Value, Range("W6:W25"), 0))
                If Application.WorksheetFunction.CountIf(Range(Cells(16, Target.Column), Cells(180, Target.Column)), cell.Value) > MaxCount Then  'Match the range extremes
                    MsgBox "This subject is already full in this option block. Please choose something else."
                    With Application
                        .EnableEvents = False
                        .Undo
                        .EnableEvents = True
                    End With
                    Exit For
                End If
            End If
        Next cell
    End Sub
    The range used for the lookup (W6:X25) has now needed to change to this:

    Excel 2016 (Windows) 32 bit
    W
    X
    Y
    5
    Option Subject
    Capacity
    6
    A Ar
    72
    7
    B Ar
    48
    8
    C CP
    24
    9
    D CP
    24
    10
    D ClasCi
    24
    11
    A DT
    48
    12
    B DT
    72
    13
    A Dr
    24
    14
    B Dr
    24
    15
    C Fr
    48
    16
    D Fr
    24
    17
    D FrAI
    24
    18
    C Gm
    24
    19
    D La
    48
    20
    A Mu
    24
    21
    B Mu
    24
    22
    C Sp
    72
    23
    D Sp
    24
    24
    D SpAI
    76
    Sheet: Y9 Options

    The options refer to:

    A D16:D180

    B E16:E180

    C F16:F180

    D G16:G180

    How can I change the code to work on each of these ranges separately? Would the lookup table be better in a different format (this can change if necessary)?

    Thanks for any help!
    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. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Hi Ali

    Would be great if you are able to upload your sample file with all the required information...Especially the values in these ranges...
    A D16:D180
    B E16:E180
    C F16:F180
    D G16:G180
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

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

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

    I can't upload a workbook at the moment unfortunately, however D16 to G180 looks like this:

    Excel 2016 (Windows) 32 bit
    D
    E
    F
    G
    15
    A B C D
    16
    Dr DT CP Sp
    17
    Dr DT Sp ClasCi
    18
    Ar DT Fr SpAI
    19
    Ar DT Fr ClasCi
    20
    DT Mu Sp CP
    21
    Ar DT Fr SpAI
    22
    Dr Ar Sp La
    23
    DT Mu Gm CP
    24
    DT Mu Sp CP
    25
    Dr Ar Gm La
    26
    Ar Dr Sp La
    27
    DT Dr Fr SpAI
    28
    Ar DT Sp ClasCi
    29
    Dr DT Sp ClasCi
    30
    Mu DT Sp ClasCi
    31
    DT Ar Sp La
    32
    DT Ar CP Fr
    Sheet: Y9 Options

    Here's the thread where I was given the code above: https://www.excelforum.com/excel-pro...tion-list.html
    Last edited by AliGW; 05-01-2019 at 01:45 PM.

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

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

    Sample workbook now attached with the code I had from the previous thread working. What I want to change is the way that the code looks up the capacity value: the values for each option column can be different. Please ask if anyone needs further clarification. Thank you!

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Perhaps...Don't see the need for a loop
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, MaxCount, aFormula
    Set rng = Range(Cells(2, Target.Column), Cells(11, Target.Column))
    If Intersect(Target, rng) Is Nothing Then Exit Sub
    If Len(Target.Value) <> 0 Then
        aFormula = "=INDEX(M2:M15,MATCH(" & Cells(1, Target.Column).Address & " & " & Target.Address & ",K2:K15&L2:L15),0)"
        MaxCount = Evaluate(aFormula)
        If Application.WorksheetFunction.CountIf(rng, Target.Value) > MaxCount Then
            MsgBox "This subject is already full in this option block. Please choose something else."
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
        End If
    End If
    End Sub
    Last edited by Sintek; 05-02-2019 at 03:09 AM.

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

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

    Thanks - I will give it a go and let you know.

    EDIT: It works perfectly on my sample file - thank you. I will now try to adapt it for the real thing.
    Last edited by AliGW; 05-02-2019 at 03:23 AM.

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

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

    OK - so it's not working as I had hoped (sorry - I cannot share the actual workbook, so bear with me).

    I have adapted the code to fit my new layout thus:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, MaxCount, aFormula
    Set rng = Range(Cells(16, Target.Column), Cells(150, Target.Column))
    If Intersect(Target, rng) Is Nothing Then Exit Sub
    If Len(Target.Value) <> 0 Then
        aFormula = "=INDEX(Y6:Y30,MATCH(" & Cells(15, Target.Column).Address & " & " & Target.Address & ",W6:W30&X6:X30),0)"
        MaxCount = Evaluate(aFormula)
        If Application.WorksheetFunction.CountIf(rng, Target.Value) > MaxCount Then
            MsgBox "This subject is already full in this option block. Please choose something else."
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
        End If
    End If
    End Sub
    Lookup table:

    Excel 2016 (Windows) 32 bit
    W
    X
    Y
    5
    Option
    Subject
    Capacity
    6
    A
    Ar
    28
    7
    B
    Ar
    48
    8
    C
    CP
    24
    9
    D
    CP
    24
    10
    D
    ClasCi
    24
    11
    A
    DT
    48
    12
    B
    DT
    72
    13
    A
    Dr
    24
    14
    B
    Dr
    24
    15
    C
    Fr
    48
    16
    D
    Fr
    24
    17
    D
    FrAI
    24
    18
    C
    Gm
    24
    19
    D
    La
    48
    20
    A
    Mu
    24
    21
    B
    Mu
    24
    22
    C
    Sp
    72
    23
    D
    Sp
    24
    24
    D
    SpAI
    76
    Sheet: Y9 Options

    Working area (there are entries down as far as row 91):

    Excel 2016 (Windows) 32 bit
    D
    E
    F
    G
    15
    A B C D
    16
    Dr DT CP Sp
    17
    Dr DT Sp ClasCi
    18
    Ar DT Fr SpAI
    19
    Ar DT Fr ClasCi
    20
    DT Mu Sp CP
    21
    Ar DT Fr SpAI
    22
    Dr Ar Sp La
    23
    DT Mu Gm CP
    24
    DT Mu Sp CP
    25
    Dr Ar Gm La
    26
    Ar Dr Sp La
    27
    DT Dr Fr SpAI
    28
    Ar DT Sp ClasCi
    29
    Dr DT Sp ClasCi
    30
    Mu DT Sp ClasCi
    31
    DT Ar Sp La
    32
    DT Ar CP Fr
    Sheet: Y9 Options

    There are currently 30 who have opted for DT in option B, and the capacity for that option is 72, however if I try to change an option in that column to DT, I get the option full message. What have I done wrong, please?
    Last edited by AliGW; 05-02-2019 at 03:56 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Can't see anything wrong offhand...Tried to mock-up a file with the info you have given but no avail...

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,026

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

    Perhaps:

    aFormula = "=LOOKUP(2,1/(W6:W30=" & Cells(15, Target.Column).Address & ")/(X6:X30=" & Target.Address & "),Y6:Y30)"
    Everyone who confuses correlation and causation ends up dead.

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Of Course...my bad...omitted the Column Ref

    EDIT...Also, forgot to declare
    MaxCount as long
    aFormula = "=INDEX(Y6:Y30,MATCH(" & Cells(15, Target.Column).Address & "&" & Target.Address & ",W6:W30&X6:X30,0),1)"
    Last edited by Sintek; 05-02-2019 at 04:54 AM.

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

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

    Thanks, chaps!

    Rory - that seems to work - thanks!

    Sintek - I wasn't sure where to put the declaration (sorry - not good with VBA).

    I will test a bit more and see if the code works properly for other columns.

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,800

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

    Its a Dimension statement that can go at the top of your Code.

    Dim MaxCount as Long
    it is currently in your code as a variant, however, so you may not need it.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

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

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

    Seems to be working really well - thank you.

  14. #14
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Seems to be working really well - thank you
    Are you referring to the corrected aformula = code Post #10

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

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

    Sorry, I wasn’t clear - I went with Rory’s variation, but thank you for your help.

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    All good...Post 10 did however solve...

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

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

    Yes - it's just that I couldn't get the declaration right, so I tried Rory's, which worked straight off. But thanks once again for your suggestion - I do appreciate it.

  18. #18
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Glad it is sorted...

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

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

    Sorry to reopen this, but I have found a slight glitch with the code I ended up using (below). I get a runtime error 13 type mismatch error if I try entering any data into the other columns of the table. How do I limit the range to columns D, E, F and G? In other words, I only want the code to execute if I am accessing cells in the range specified (rows 15 to 150) in columns D to G. Hope this is clear.

    Thanks for any help.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, MaxCount, aFormula
    ' SINTEK: MaxCount As Long
    Set rng = Range(Cells(16, Target.Column), Cells(150, Target.Column))
    If Intersect(Target, rng) Is Nothing Then Exit Sub
    If Len(Target.Value) <> 0 Then
        aFormula = "=LOOKUP(2,1/(W6:W30=" & Cells(15, Target.Column).Address & ")/(X6:X30=" & Target.Address & "),Y6:Y30)"
        ' SINTEK: aFormula = "=INDEX(Y6:Y30,MATCH(" & Cells(15, Target.Column).Address & " & " & Target.Address & ",W6:W30&X6:X30),1)"
        MaxCount = Evaluate(aFormula)
        If Application.WorksheetFunction.CountIf(rng, Target.Value) > MaxCount Then
            MsgBox "This subject is already full in this option block. Please choose something else."
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
        End If
    End If
    End Sub
    Last edited by AliGW; 05-25-2019 at 05:41 AM.

  20. #20
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Hi AliGw

    Quite tricky trying to remember what was actually being achieved...
    Set rng = Range(Cells(16, 4), Cells(150, 7))

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

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

    Perfect - thanks!

  22. #22
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,964

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

    Pleasure...

+ 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. [SOLVED] VBA to Prevent Data Entry in Cell with Data Validation List
    By AliGW in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 03-26-2019, 03:02 PM
  2. Replies: 3
    Last Post: 05-09-2016, 08:27 PM
  3. Prevent Data Entry using Data Validation
    By HangMan in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-24-2015, 03:01 PM
  4. [SOLVED] Data Validation: How to clear/delete the content of the cell and not Data Validation List?
    By lukelucky in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2015, 09:42 AM
  5. [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
  6. Replies: 4
    Last Post: 12-19-2013, 10:44 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