1. 程式人生 > >Excel2013 單元格中 新增可以多選的下拉框

Excel2013 單元格中 新增可以多選的下拉框

首先弄好 下拉單選的,[data(資料)-- data validation(資料驗證) -- 選 list (列表)-- 輸入 你的所有選項,用英文逗號分割開]
然後--開發工具(預設隱藏的,在Excel的file(檔案)--options(選項)--customize ribbon--勾選 developer)--view code
在開啟的 VBA中,貼上下面的程式碼,點選儲存按鈕,提示框點選確定。



程式碼來自:劉L the Blog

------------ 我是分割線,不要複製我----------------
Option Explicit


Sub Worksheet_Change(ByVal Target As Range)
'讓資料有效性選擇 可以多選,重複選
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
If Target.Count > 1 Then GoTo exitHandler


On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
Else
If newVal = "" Then
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If


exitHandler:
Application.EnableEvents = True
End Sub
------------ 我是分割線,不要複製我----------------