VBA code not working
On May 21,2022 by Tom RoutleyI have the following VBA code :
Issue
Private Sub Worksheet_Change(ByVal Target As Range) Dim cRow As Integer If Intersect(Target, Range("R11:R20")) Is Nothing Then Exit Sub If Target.Value = "F" Then ActiveSheet.Unprotect cRow = Target.Row Range(Cells(cRow, "T"), Cells(cRow, "U")).Locked = True ActiveSheet.Protect End If If Target.Value = "T" Or Target.Value = "" Then ActiveSheet.Unprotect cRow = Target.Row Range(Cells(cRow, "T"), Cells(cRow, "U")).Locked = False ActiveSheet.Protect End If End Sub
What I need is to ClearContents for (cRow target.Row) in the upper case first before it is locked.3
I have Excel sheet to calculate overtime, includes several rows for each month, and the sheet is protected but allowing the user to enter data into some specified range (P11:P20,R11:X20) (Allow user to edit range).
Also, the Range (R11:R20) are rows with drop list menu of 4 values (1,2,3,4).
My case: For example, if the user selected either value 3 or 4 ONLY in cell (R15) I need to block or protect the range (T15:U15), and if then with the same sheet selected 3 or 4 value in cell R19 to protect the range (T19:U19) and so on.
Therefore I need if some value selected in specific cell to protect some ranges within the same row. Please note that the user may select one selection or multi selection in the same sheet.
Solution
For your current query I assumed you want to clear the contents of column T and U of the target row.
Private Sub Worksheet_Change(ByVal Target As Range) Dim cRow As Integer If Intersect(Target, Range("R11:R20")) Is Nothing Then Exit Sub If Target.Value = "F" Then ActiveSheet.Unprotect cRow = Target.Row With Range(Cells(cRow, "T"), Cells(cRow, "U")) .ClearContents .Locked = True End With ActiveSheet.Protect End If If Target.Value = "T" Or Target.Value = "" Then ActiveSheet.Unprotect cRow = Target.Row Range(Cells(cRow, "T"), Cells(cRow, "U")).Locked = False ActiveSheet.Protect End If End Sub
Thanks to TrowaD for this tip.
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply