bluewomble88
New Member
- Joined
- Jun 7, 2017
- Messages
- 3
I am really bad with VBA and would appreciate some help.
I have a sheet that has multiple columns (let's say A through to G) and it's critical that some columns (e.g. B) are protected as they return data automatically using VLOOKUP. My problem is that columns D and G have drop-down lists assigned via Data Validation and I need the user to be able to make more than 1 selection. I have found some code that does this but only for one column, not both. How do I fix this?
The other issue is that even with one column working, as soon as I protect the sheet, I can't filter the columns because of this code. I am losing my mind TBH.
I need to be able to have the sheet protected, columns D and G to allow multiple selections in the drop-down lists, and the data to be filtered if I want. Can this be done?
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 4 Then
ActiveSheet.Unprotect
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & vbNewLine & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
ActiveSheet.Protect
Application.EnableEvents = True
End Sub
I have a sheet that has multiple columns (let's say A through to G) and it's critical that some columns (e.g. B) are protected as they return data automatically using VLOOKUP. My problem is that columns D and G have drop-down lists assigned via Data Validation and I need the user to be able to make more than 1 selection. I have found some code that does this but only for one column, not both. How do I fix this?
The other issue is that even with one column working, as soon as I protect the sheet, I can't filter the columns because of this code. I am losing my mind TBH.
I need to be able to have the sheet protected, columns D and G to allow multiple selections in the drop-down lists, and the data to be filtered if I want. Can this be done?
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from Online Excel Tips & Tutorials
' To allow multiple selections in a Drop Down List in Excel (without repetition)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 4 Then
ActiveSheet.Unprotect
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & vbNewLine & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
ActiveSheet.Protect
Application.EnableEvents = True
End Sub