VBA Nested if statement to lock cells base on cell value

SeliM

Board Regular
Joined
Aug 10, 2023
Messages
62
Office Version
  1. 365
Platform
  1. Windows
I have an audit tool with a drop down selection of several service types. For two service types there are additional audit questions of varying sizes. I have been attempting to build script the provides for certain cells to be locked as they do not apply and for the two service types a range of cells to be locked when other service types are chosen.

For example "Community Nurse" and "Home Care Packages" have additional questions that do not apply to other services.
I have one script i found that helps on a new workbook:
Private Sub Worksheet_Change(ByVal Target As Range)
If [B3] = "Community Nurse" Then
[C40:C41].Locked = True
Else
[C40:C41].Locked = False
End If
End Sub

When I have use in my Workbook it does not work. I also cannot work through how to nest the If Statement to include "Home Care Packages."


Any guidance would be most appreciated.

Mel
ps I couldn't load the XL2bb system said the machine is in protect mode!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the Board!

Are you sure that you posted that code in the Sheet module of the sheet that you want to apply it to in VBA?
If you place it any other VBA module, it will not run automatically.
If you are not sure, go to the sheet that you want to apply it to, right-click on the sheet tab name at the bottom of the screen, select View Code, and post the VBA code in the VB Editor window that pops up there.

Also, you need to make sure that you have VBA enabled.
 
Upvote 0
Locking or unlocking a cell will do nothing until you turn protection on.
See if this gives you any ideas.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    ' Only perform the code if Cell B3 has been changed
    If Intersect(Target, Range("B3")) Is Nothing Then Exit Sub
    Me.Unprotect Password:="xxx"
    
    If [B3] = "Community Nurse" Then
        [C40:C41].Locked = True
    ElseIf [B3] = "Home Care Packages" Then
        [D40:D41].Locked = True
    Else
        [C40:C41].Locked = False
        [D40:D41].Locked = False
    End If
    
    Me.Protect Password:="xxx"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
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