I am creating code on a protected sheet that utilizes the intersect function.
I want the entire sheet to be locked EXCEPT Column B. Column B has drop down options for users to select and once they select one it locks that cell and also records the date & their user name in columns C & D. All other empty cells in column B will remain unlocked (I'm struggling with this as well).
Now when a specific option is selected by the user I need to copy that entire row and paste the entire copied cell directly below the original but I need to delete the contents in B, C, D so that it becomes an empty unlocked cell.
However, I've noticed my code is restarting my sub because I'm making a change to column B, and it keeps adding the row endlessly. Does anyone have any suggestions on how to fix this and/or to do this better?
Thank you for any and all help!
I want the entire sheet to be locked EXCEPT Column B. Column B has drop down options for users to select and once they select one it locks that cell and also records the date & their user name in columns C & D. All other empty cells in column B will remain unlocked (I'm struggling with this as well).
Now when a specific option is selected by the user I need to copy that entire row and paste the entire copied cell directly below the original but I need to delete the contents in B, C, D so that it becomes an empty unlocked cell.
However, I've noticed my code is restarting my sub because I'm making a change to column B, and it keeps adding the row endlessly. Does anyone have any suggestions on how to fix this and/or to do this better?
Thank you for any and all help!
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range, c As Range, d As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
If Target.Columns.Count = Me.Columns.Count Then Exit Sub
Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target) 'sets WorkRng to be on alert for changes in column B
xOffsetColumn = 1 'constant definition
If Not WorkRng Is Nothing Then 'looking for a change in column B
Target.Worksheet.Unprotect Password:="1234" 'unprotecting the sheet
Application.EnableEvents = False 'holding off other actions
For Each Rng In WorkRng
Set c = Rng.Offset(0, xOffsetColumn) 'assigning range value to c
Set d = Rng.Offset(0, xOffsetColumn + 1) 'assigning range value to d
If Not VBA.IsEmpty(Rng.Value) Then 'checking
c.Value = Now
c.NumberFormat = "dd-mm-yyyy, hh:mm:ss"
d.Value = Application.UserName
Else
c.ClearContents
End If
Next
Application.EnableEvents = True
Target.Locked = True
Target.Worksheet.Protect Password:="1234", Userinterfaceonly:=True, AllowFiltering:=True
Set Temp = Target
End If
If InStr(1, Temp, ",", vbTextCompare) > 0 Then ' checking to see if the string contains a comma (the options with a comma are done and can be locked)
Temp.Locked = True 'lock the target cell
Else
'Entering loop where the comma doesn't exist
Temp.Locked = False 'keeping target cell unlocked
Application.EnableEvents = False 'holding off other actions to attempt removing loop
'MsgBox Temp.Address
Rows(Temp.Row & ":" & Temp.Row).Select 'selecting the row target was in
Selection.Copy 'copy range from above
Rows(Temp.Row + 1 & ":" & Temp.Row + 1).Select 'selecting the row right below where target was
Selection.Insert Shift:=xlDown 'shifting rows down
Range("B" & Temp.Row + 1 & ":" & "D" & Temp.Row + 1).Select 'selecting B#:D# within new row
Application.EnableEvents = True 'allowing actions again
Application.CutCopyMode = False
Selection.ClearContents 'deleteing contents of aforementioned cells
'Set Temp = Cells(Temp.Row + 1 & Temp.Column) ' another attempt to prevent looping
End If
End Sub