VBA and dropdown list: a case of "only choice"

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I've set a dropdown list as follows:

https://imgur.com/a/ntDkIK9

Code:
Dim MyListB(2) As String
MyListB(0) = "YES"
MyListB(1) = "NO"

Dim lrut2 As Long
    lrut2 = Cells(Rows.Count, "A").End(xlUp).Row

With Range("B2:B" & lrut2).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
         Operator:=xlBetween, Formula1:=Join(MyListB, ",")
End With

For each record in column A, the user can select YES or NO.

The complication is:

some of the choices are not possible, meaning that if one record in column A is also present in column J, the only choice in column B is NO, so I have to set it in advance not allowing the user any action.

So, in this case, the value cells in B3, B4, B8 and B11 has to be "NO".

How could I manage this scenario?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hello everybody.

I've set a dropdown list as follows:

https://imgur.com/a/ntDkIK9

Code:
Dim MyListB(2) As String
MyListB(0) = "YES"
MyListB(1) = "NO"

Dim lrut2 As Long
    lrut2 = Cells(Rows.Count, "A").End(xlUp).Row

With Range("B2:B" & lrut2).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
         Operator:=xlBetween, Formula1:=Join(MyListB, ",")
End With

For each record in column A, the user can select YES or NO.

The complication is:

some of the choices are not possible, meaning that if one record in column A is also present in column J, the only choice in column B is NO, so I have to set it in advance not allowing the user any action.

So, in this case, the value cells in B3, B4, B8 and B11 has to be "NO".

How could I manage this scenario?

Any ideas?
 
Upvote 0
Try this "Change Event" code:-
Paste code in Activesheet code module
Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_Change(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
Application.EnableEvents = False
[COLOR="Navy"]If[/COLOR] Not Target.SpecialCells(xlCellTypeAllValidation) [COLOR="Navy"]Is[/COLOR] Nothing And Target.Column = 2 [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("J2", Range("J" & Rows.Count).End(xlUp))
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] Target.Offset(, -1).Value = Dn.Value [COLOR="Navy"]Then[/COLOR]
            Target.Value = "No"
            [COLOR="Navy"]Exit[/COLOR] For
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] If
Application.EnableEvents = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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