Excel 多选下拉列表

Excel 多选下拉列表,excel,vba,excel-formula,Excel,Vba,Excel Formula,这是多选下拉列表的代码,但当我从下拉列表中选择多个项目时,它会在同一单元格中打印所选项目Target.value,并用逗号分隔。但我想将这些项目打印到另一个单元格中,比如左单元格或右单元格或任何需要的单元格值,并用逗号分隔。并且应该能够选择一次项目 Private Sub Worksheet_Change(ByVal Target As Range) 'Updated by Extendoffice 2019/11/13 Dim xRng As Range Dim xVa

这是多选下拉列表的代码,但当我从下拉列表中选择多个项目时,它会在同一单元格中打印所选项目
Target.value
,并用逗号分隔。但我想将这些项目打印到另一个单元格中,比如左单元格右单元格或任何需要的单元格值,并用逗号分隔。并且应该能够选择一次项目

Private Sub Worksheet_Change(ByVal Target As Range)
    'Updated by Extendoffice 2019/11/13
    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 xValue1 = xValue2 Or _
                   InStr(1, xValue1, ", " & xValue2) Or _
                   InStr(1, xValue1, xValue2 & ",") Then
                    Target.Value = xValue1
                Else
                    Target.Value = xValue1 & ", " & xValue2
                End If
            End If
        End If
    End If
    Application.EnableEvents = True
End Sub 
Private子工作表\u更改(ByVal目标作为范围)
“延期办公室2019/11/13更新
Dim xRng As范围
作为字符串的Dim xValue1
Dim xValue2作为字符串
如果Target.Count>1,则退出Sub
出错时继续下一步
设置xRng=Cells.SpecialCells(xlCellTypeAllValidation)
如果xRng为空,则退出Sub
Application.EnableEvents=False
如果不是,则Application.Intersect(Target,xRng)什么都不是
xValue2=目标值
应用程序。撤消
xValue1=目标值
Target.Value=xValue2
如果xValue1“”,则
如果xValue2“”,则
如果xValue1=xValue2或_
仪表(1,xValue1,“,”和xValue2)或_
仪表(1,xValue1,xValue2&“,”)然后
Target.Value=xValue1
其他的
Target.Value=xValue1&“,”和xValue2
如果结束
如果结束
如果结束
如果结束
Application.EnableEvents=True
端接头

如果要将结果打印到相邻单元格,请查看偏移量