Extend VBA to further columns

DHAM1963

New Member
Joined
Jan 25, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi, I am not very experienced with VBA so this might be easy. I have the VBA code below which works perfectly for Column B. I know need to do the exact same thing for columns C to AF. Is a nested if statement the way to go. So each column is a separate criteria If B38 is more than 5 lock column B, is C38 is more than 5 lock column C, If D38 is more than 5 lock D. Thanks in advance for any help.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Unprotect "pw"
If Range("B38") < 5 Then
Range("B3:B37").Locked = False
Else
Range("B3:B37").Locked = True
End If
ActiveSheet.Protect "pw"
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,

Please check below code:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Unprotect "pw"
    For colno = 2 To 32
        If Cells(38, colno) < 5 Then
            Range(Cells(3, colno), Cells(37, colno)).Locked = False
        Else
            Range(Cells(3, colno), Cells(37, colno)).Locked = True
        End If
    Next
    ActiveSheet.Protect "pw"
End Sub
 
Upvote 0
Solution
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ActiveSheet.Unprotect "pw" For colno = 2 To 32 If Cells(38, colno) < 5 Then Range(Cells(3, colno), Cells(37, colno)).Locked = False Else Range(Cells(3, colno), Cells(37, colno)).Locked = True End If Next ActiveSheet.Protect "pw" End Sub
Thank you so much, this solution worked perfectly, and gives me some code to work off of for some other similar things I want to do.
 
Upvote 0
That's Great. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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