Hi - this nice VBA allows multiple selections from a drop-down.
I was hoping I could get it work on a protected sheet by adding:
after
How can it be modified to work in protected sheet?
I was hoping I could get it work on a protected sheet by adding:
ActiveSheet.Unprotect
before Application.EnableEvents = False
and ActiveSheet.Protect
after
Application.EnableEvents = True
...but that didn't work: The sheet remained protected the next time I tried to to select from the drop-down in another cell in DV_Range.How can it be modified to work in protected sheet?
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Originally from Phil Treacy see https://www.myonlinetraininghub.com/select-multiple-items-from-drop-down-data-validation-list
Dim OldVal As String
Dim NewVal As String
' If more than 1 cell is being changed
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Not Intersect(Target, ActiveSheet.Range("DV_Range")) Is Nothing Then
' Turn off events so our changes don't trigger this event again
ActiveSheet.Unprotect
Application.EnableEvents = False
NewVal = Target.Value
' If there's nothing to undo this will cause an error
On Error Resume Next
Application.Undo
On Error GoTo 0
OldVal = Target.Value
' If selection is already in the cell we want to remove it
If InStr(OldVal, NewVal) Then
'If there's a comma in the cell, there's more than one word in the cell
If InStr(OldVal, ",") Then
If InStr(OldVal, ", " & NewVal) Then
Target.Value = Replace(OldVal, ", " & NewVal, "")
Else
Target.Value = Replace(OldVal, NewVal & ", ", "")
End If
Else
' If we get to here the selection was the only thing in the cell
Target.Value = ""
End If
Else
If OldVal = "" Then
Target.Value = NewVal
Else
' Delete cell contents
If NewVal = "" Then
Target.Value = ""
Else
' This IF prevents the same value appearing in the cell multiple times
' If you are happy to have the same value multiple times remove this IF
If InStr(Target.Value, NewVal) = 0 Then
Target.Value = OldVal & ", " & NewVal
End If
End If
End If
End If
Application.EnableEvents = True
Else
Exit Sub
ActiveSheet.Protect
End If
End Sub