在Excel的下拉列表中允许多个选择

时间:2019-03-18 07:13:18

标签: excel vba

我为Excel编写了VBA代码,用于选择多个项目。它选择了几个项目,并且没有重复。当前,当我选择一个项目时,它会删除所有项目。如何更改代码,使其仅删除所选的代码?

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Oldvalue As String Dim Newvalue As String
   Application.EnableEvents = True On Error GoTo Exitsub If
   Target.Column = 9 Or Target.Column = 18 Then

     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 & Chr(10) & Newvalue
         Else:
           Target.Value = Replace(oldVal, newVal & vbLf, "")
         End If
       End If   
     End If 
  End If 
  Application.EnableEvents = True Exitsub: 
  Application.EnableEvents = True 
End Sub

0 个答案:

没有答案