I have a VBA Code to Lock cells which is not working as desired.
In cell Range I9 to BI9 I have values which are Lock/Unlock. So based on these values, where ever the value Lock, it should lock the range I9 to I300.
In cell range B21 to B300, I have values Lock/Unlock. Based on these values where ever the value Lock, it should lock the row from I21 to BI21.
Problem I am facing is either the rows would get locked or columns. It does not work in coordination.
I want it to work as tabulated:
Row Column Value
Lock Lock Locked
Lock Unlock Locked
Unlock Lock Locked
Unlock Unlock Unlocked
Here is my code.
Private Sub Workbook_Open()
Dim sh As Object
Dim sheetnames As String
Dim i As Integer
Dim col As Range
Dim rng As Range: Set rng = Application.Range("I16:BI16")
Application.ScreenUpdating = False
For Each sh In Sheets 'First Each
If sh.Name <> Warning Then 'Warning If
sh.Visible = xlSheetVisible
If sh.Name <> "Configuration" Then 'Configuration If
sheetnames = sh.Name
Worksheets(sheetnames).Activate
ActiveSheet.Unprotect Password:="xxx"
For Each col In rng.Columns
If col.Columns.Value = "Lock" Then
ActiveSheet.Range(Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "22:" & Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "300").Locked = True
ActiveSheet.Range(Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "22:" & Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "300").Font.Color = -16776961
Else
ActiveSheet.Range(Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "22:" & Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "300").Locked = False
ActiveSheet.Range(Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "22:" & Replace(Cells(1, col.Column).Address(0, 0), 1, "") & "300").Font.Color = vbBlack
End If
Next col
For i = 22 To 300 'Lock rows with Total and VAS activity
If sh.Range("B" & i).Value = "Lock" Then
sh.Range("I" & i & ":" & "BI" & i).Locked = True
sh.Range("C" & i & ":BI" & i).Font.Color = -16776961
Else
sh.Range("I" & i & ":BI" & i).Locked = False
sh.Range("C" & i & ":" & "BI" & i).Font.ColorIndex = xlAutomatic
End If
Next i
End If 'End of Configuration If
End If 'End of Warning if
Next sh 'End of First Each
ActiveSheet.Protect Password:="xxx"
Sheets(1).Select
End Sub