Excel(VBA)下拉列表单个单元格中的多个值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel(VBA)下拉列表单个单元格中的多个值相关的知识,希望对你有一定的参考价值。

Selecting different values from a data validation drop down list will populate the same cell, i.e. it will create a concatenating list separated by commas.
Amend the "If Target.Column" values to set the columns this applies to.
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. ' Developed by Contextures Inc.
  3. ' www.contextures.com
  4. Dim rngDV As Range
  5. Dim oldVal As String
  6. Dim newVal As String
  7. If Target.Count > 1 Then GoTo exitHandler
  8.  
  9. On Error Resume Next
  10. Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
  11. On Error GoTo exitHandler
  12.  
  13. If rngDV Is Nothing Then GoTo exitHandler
  14.  
  15. If Intersect(Target, rngDV) Is Nothing Then
  16. 'do nothing
  17. Else
  18. Application.EnableEvents = False
  19. newVal = Target.Value
  20. Application.Undo
  21. oldVal = Target.Value
  22. Target.Value = newVal
  23. If Target.Column = 3 Then
  24. If oldVal = "" Then
  25. 'do nothing
  26. Else
  27. If newVal = "" Then
  28. 'do nothing
  29. Else
  30. Target.Value = oldVal _
  31. & ", " & newVal
  32. ' NOTE: you can use a line break,
  33. ' instead of a comma
  34. ' Target.Value = oldVal _
  35. ' & Chr(10) & newVal
  36. End If
  37. End If
  38. End If
  39. End If
  40.  
  41. exitHandler:
  42. Application.EnableEvents = True
  43. End Sub

以上是关于Excel(VBA)下拉列表单个单元格中的多个值的主要内容,如果未能解决你的问题,请参考以下文章

Excel公式计算单个单元格中的多个可能组合

VBA excel Target.Address =单元格范围

Excel VBA - 将多个用户表单复选框值写入单个单元格

使用 Excel VBA 将多个值插入连接查询

使用VBA在Excel填充单元格中插入多个图像,但保持宽高比

Excel 2010 VBA:使用单元格中的值保存文件以确定路径和文件名