1

When I am currently applying this code to allow for multiple selection in my dropdown menu I want it to only affect Column E. However, it have an affect on the whole sheet. Can some body please help me to fix this, so I can isolate the code?

Private Sub Worksheet_Change(ByVal Target As Range)
' 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
Dim AffectedCells As Range
Set AffectedCells = Intersect(Me.Range("$E$2:$E$40"), Target)
  If Target 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 & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub••••ˇˇˇˇ

For example when I write in Column D and wish to overwrite it. It just makes a conjunction between what previously was in the box and the newly added information.

Picture example

Thanks a lot.

kamsteg
  • 31
  • 4
  • 1
    You are just setting this `Set AffectedCells = Intersect(Me.Range("$E$2:$E$40"), Target)` but you are not narrowing your code to work only on Col E. you have to put your code inside `If not Intersect(Range("$E$2:$E$40"), Target) is nothing then` You may want to see [THIS](https://stackoverflow.com/questions/13860894/why-ms-excel-crashes-and-closes-during-worksheet-change-sub-procedure/13861640#13861640) – Siddharth Rout Jun 09 '21 at 13:31
  • After `Set AffectedCells = Intersect(Me.Range("$E$2:$E$40"), Target)` you need to work with `AffectedCells`, not `Target` Also worth adding `If AffectedCells.Cells.Count>1 Then Exit Sub` or your code will fail if >1 cell are changed. – Tim Williams Jun 09 '21 at 15:19

0 Answers0