By kgardner miercuri, 13 iulie 2022
postat în Excel
Răspunsuri 3
Preferințe 1
Vizualizări 5.7 K
Voturi 0
I modified the subject function to remove existing selections on re-selecting them and to remove extra ;'s. Here's the revised code:

Subsol de lucrări private_Change (țintă ByVal ca rază de acțiune)
„Actualizat de Extendoffice 2019/11/13
'Updated by Ken Gardner 2022/07/11
Dim xRng As Range
Dim xValue1 As String
Dim xValue2 As String
Dim semiColonCnt As Integer
Dacă Target.Count > 1, apoi Ieșiți din sub
La data de eroare CV următoare
Set xRng = Cells.SpecialCells(xlCellTypeAllValidation)
If xRng Is Nothing Then Exit Sub
Application.EnableEvents = Fals
'If Not Application.Intersect(Target, xRng) Is Nothing Then
If Application.Intersect(Target, xRng) Then
xValue2 = Target.Value
Aplicație.Anulați
xValue1 = Target.Value
Target.Value = xValue2
If xValue1 <> "" Then
If xValue2 <> "" Then
If xValue1 = xValue2 Or xValue1 = xValue2 & ";" Or xValue1 = xValue2 & "; " Then ' leave the value if only one in list
xValue1 = Replace(xValue1, "; ", "")
xValue1 = Replace(xValue1, ";", "")
Target.Value = xValue1
ElseIf InStr(1, xValue1, "; " & xValue2) Then
xValue1 = Replace(xValue1, xValue2, "") ' removes existing value from the list on repeat selection
Target.Value = xValue1
ElseIf InStr(1, xValue1, xValue2 & ";") Then
xValue1 = Replace(xValue1, xValue2, "")
Target.Value = xValue1
Altfel
Target.Value = xValue1 & "; " & xValue2
Final, dacă
Target.Value = Replace(Target.Value, ";;", ";")
Target.Value = Replace(Target.Value, "; ;", ";")
If InStr(1, Target.Value, "; ") = 1 Then ' check for ; as first character and remove it
Target.Value = Replace(Target.Value, "; ", "", 1, 1)
Final, dacă
If InStr(1, Target.Value, ";") = 1 Then
Target.Value = Replace(Target.Value, ";", "", 1, 1)
Final, dacă
semiColonCnt = 0
For i = 1 To Len(Target.Value)
If InStr(i, Target.Value, ";") Then
semiColonCnt = semiColonCnt + 1
Final, dacă
Apoi eu
If semiColonCnt = 1 Then ' remove ; if last character
Target.Value = Replace(Target.Value, "; ", "")
Target.Value = Replace(Target.Value, ";", "")
Final, dacă
Final, dacă
Final, dacă
Final, dacă
Application.EnableEvents = Adevărat
End Sub
Hi Ken Gardner,

Thanks for your sharing. Do you mind if we add your VBA code to our tutorial: How To Create Drop Down List With Multiple Selections Or Values In Excel?

Aștept cu nerăbdare să ne auzim.

Amanda
·
1 ani în urmă
·
1 Îi place
·
0 Voturi
·
4 Comentarii
·
Hi Amanda, by all means go ahead. I did get the original code from ExtendOffice.
Cheers, Ken
·
1 ani în urmă
·
1 Îi place
·
0 Voturi
·
4 Comentarii
·
Cheers Ken
·
1 ani în urmă
·
1 Îi place
·
0 Voturi
·
4 Comentarii
·
Vizualizați mesajul complet