Worksheet change help needed

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
303
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet which contains seven dropdown boxes.

There are two asks I am struggling with.

Firstly, if the user selects "At fault" in cell B16 then I need cell B21 to populate with "Yes" and cells B22-B26 to be locked. If the user subsequently selects something different in cell B16 then cell B21 is cleared and cells B22-B26 become unlocked.

Secondly, if the user selects anything other than "At fault" in cell B16, cell B21 is locked and they can't select it (they can only select cells B22-B26).

I have been trying worksheet change events with "Target Range" and "Application Intersect" but can't get it to work.

Any advice greatly appreciated as always.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I think your explanation may be missing a few details such as:
- should cell B21 become unlocked in some scenarios?
- should cell B21 be cleared out in any scenarios?

In any event, that can easily enough be added to the code below.

Note before you use this code, you must remove the "Locked" property of all cells.
You can do this by selecting all cells, then right-click on any cell and select "Format Cells"
Go to the "Protection" tab, and uncheck the "Locked" box.
1716379091317.png


Then, this Worksheet_Change code should do what you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Check to see if cell B16 updated, and if not, exit sub
    If Intersect(Target, Range("B16")) Is Nothing Then Exit Sub
   
    Application.EnableEvents = False
   
'   Check value of cell B16
    Select Case Range("B16").Value
'       If "At fault" is entered in cell B16
        Case "At fault"
'           Set value of cell B21
            Range("B21") = "Yes"
'           Lock cells B22:B26
            ActiveSheet.Unprotect
            Range("B22:B26").Locked = True
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
'       If anything else entered in cell B16
        Case Else
'           Lock cell B21 and unlock cells B22:B26
            ActiveSheet.Unprotect
            Range("B21").Locked = True
            Range("B22:B26").Locked = False
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Select
   
    Application.EnableEvents = True

End Sub
 
Upvote 1
Solution
Thanks for that, much appreciated.

Upon opening, all cells will be unlocked.

Cell B21 only needs to be locked if the user selects anything other than "At fault" in cell B16.
 
Upvote 0
Cell B21 only needs to be locked if the user selects anything other than "At fault" in cell B16.
Right, but does it actually need to become unlocked if cell B16 is "At fault"?
I wasn't sure, as you are already populating it with a "Yes" if B16 is set to "At fault".

Anyway, did you try the code and see how it works for you?
 
Upvote 1
Right, but does it actually need to become unlocked if cell B16 is "At fault"?
I wasn't sure, as you are already populating it with a "Yes" if B16 is set to "At fault".

Anyway, did you try the code and see how it works for you?
I'll give it a go this afternoon, many thanks again. I will feedback.
 
Upvote 0
Right, but does it actually need to become unlocked if cell B16 is "At fault"?
I wasn't sure, as you are already populating it with a "Yes" if B16 is set to "At fault".

Anyway, did you try the code and see how it works for you?
Thanks for your help, everything works the way I need it to :-)
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
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