У меня всего 6 выпадающих списков в моем Excel. В них мне нужно несколько раз выбрать и удалить только для определенных выпадающих списков. Я использовал код VBA из Интернета. Когда я использую этот код, множественный выбор и удаление применяется ко всем раскрывающимся спискам, но я хочу, чтобы он применялся только к определенным целевым раскрывающимся спискам. Как установить раскрывающийся список целей для множественного выбора и удаления с помощью кода VBA?
У меня есть следующие столбцы (Категория и Регион) в моем приложении Excel. Я хочу ограничить выбор только одним, где пользователь может выбрать только одну категорию - A, B, C, но может иметь несколько вариантов выбора и удаления для столбца Region.
CATEGORY REGION
A USA
B MEX
C GER
Это код VBA, который я использовал.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
If Target.Count > 1 Then Exit Sub
On Error Resume Next
Set xRng = Cells.SpecialCells(xlCellTypeAllValidation)
If xRng Is Nothing Then Exit Sub
Application.EnableEvents = False
If Not Application.Intersect(Target, xRng) Is Nothing Then
xValue2 = Target.Value
Application.Undo
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" Then
If xValue2 <> "" Then
If InStr(1, xValue1, xValue2 & ",") > 0 Then
xValue1 = Replace(xValue1, xValue2 & ", ", "") ' If it's in the middle with comma
Target.Value = xValue1
GoTo jumpOut
End If
If InStr(1, xValue1, ", " & xValue2) > 0 Then
xValue1 = Replace(xValue1, ", " & xValue2, "") ' If it's at the end with a comma in front of it
Target.Value = xValue1
GoTo jumpOut
End If
If xValue1 = xValue2 Then ' If it is the only item in string
xValue1 = ""
Target.Value = xValue1
GoTo jumpOut
End If
Target.Value = xValue1 & ", " & xValue2
End If
jumpOut:
End If
End If
Application.EnableEvents = True
End Sub