Excel 單元格下拉復(fù)選框(多選項(xiàng))——VBA 學(xué)習(xí)

我們在制作excel表格模板的時候,為了控制用戶的輸入,會將單元格設(shè)置數(shù)據(jù)驗(yàn)證格式,如下圖:

單選

但數(shù)據(jù)驗(yàn)證只支持單選項(xiàng),無法支持多選,那么,我們?nèi)绾螌?shí)現(xiàn)下拉復(fù)選框,實(shí)現(xiàn)多個選項(xiàng)勾選呢?(如下圖)


下拉復(fù)選框

實(shí)驗(yàn)開始:

環(huán)境:Excel 2016

支持:VBA支持庫(沒有安裝的,需要安裝哦)

第一步:準(zhǔn)備數(shù)據(jù)源

新建一個Excel表格,sheet1創(chuàng)建4列表頭字段,sheet2用來存放選項(xiàng)值,我們以“愛好”、“學(xué)習(xí)課程”為例來實(shí)現(xiàn)下拉復(fù)選框


sheet1


sheet2

第二步:調(diào)出開發(fā)工具

文件 >> 選項(xiàng) >> 勾選 “開發(fā)工具”,點(diǎn)擊“確定”。


第三步:插入列表框控件

開發(fā)工具 >> 插入 >> ActiveX 控件? >> 列表框控件,繪制控件。

第四步:設(shè)置列表框控件屬性

如下圖,繪制好的列表框控件名稱默認(rèn)為“ListBox1”,當(dāng)前為“設(shè)計模式”,點(diǎn)擊“屬性”,進(jìn)入設(shè)置。

在彈出的屬性設(shè)置框中,設(shè)置好樣式、多選、選項(xiàng)值的數(shù)據(jù)源范圍(即sheet2 "愛好"一列的數(shù)據(jù),不包括表頭)。

用同樣的方法,新添加一個列表框控件,注意第二個列表框?yàn)長istBox2,并設(shè)置相關(guān)屬性,多選項(xiàng)的數(shù)據(jù)源范圍為“Sheet2!B2:B8”。


第五步:啟用VBA代碼

在狀態(tài)欄找到并點(diǎn)擊“查看代碼”,或是在活動表Sheet1右擊,選擇“查看代碼”,進(jìn)入VBA編輯器。

注:如果Excel沒有安裝VBA支持庫的,需要先安裝好哦~

將下方代碼復(fù)制,并調(diào)試編譯


Private Sub ListBox1_Change()

If Reload Then Exit Sub '加載ListBox1

For i = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(i) = True Then t = t & "," & ListBox1.List(i)

Next

ActiveCell = Mid(t, 2)

End Sub

Private Sub ListBox2_Change()

If Reload Then Exit Sub '加載ListBox2

For i = 0 To ListBox2.ListCount - 1

If ListBox2.Selected(i) = True Then t = t & "," & ListBox2.List(i)

Next

ActiveCell = Mid(t, 2)

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

With ListBox1

? ? ? ? '第 2 列 且 單元格大于 1,因?yàn)楸眍^的字段不需要進(jìn)行多選

? ? ? ? If ActiveCell.Column = 2 And ActiveCell.Row > 1 Then

? ? ? ? ? ? t = ActiveCell.Value

? ? ? ? ? ? Reload = True '如果是根據(jù)單元格的值修改列表框,則暫時屏蔽listbox的change事件。

? ? ? ? ? ? For i = 0 To .ListCount - 1 '根據(jù)活動單元格內(nèi)容修改列表框中被選中的內(nèi)容

? ? ? ? ? ? ? ? If InStr(t, .List(i)) Then

? ? ? ? ? ? ? ? ? ? .Selected(i) = True

? ? ? ? ? ? ? ? ? ? Else

? ? ? ? ? ? ? ? ? ? .Selected(i) = False

? ? ? ? ? ? ? ? End If

? ? ? ? ? ? Next

? ? ? ? ? ? Reload = False

? ? ? ? ? ? .Top = ActiveCell.Top + ActiveCell.Height '以下語句根據(jù)活動單元格位置顯示列表框

? ? ? ? ? ? .Left = ActiveCell.Left

? ? ? ? ? ? .Width = ActiveCell.Width

? ? ? ? ? ? .Visible = True

? ? ? ? ? ? Else

? ? ? ? ? ? .Visible = False

? ? ? ? End If

? ? End With

With ListBox2

? ? ? ? '第 4 列 且 單元格大于 1,因?yàn)楸眍^的字段不需要進(jìn)行多選

? ? ? ? If ActiveCell.Column = 4 And ActiveCell.Row > 1 Then

? ? ? ? ? ? t = ActiveCell.Value

? ? ? ? ? ? Reload = True '如果是根據(jù)單元格的值修改列表框,則暫時屏蔽listbox的change事件。

? ? ? ? ? ? For i = 0 To .ListCount - 1 '根據(jù)活動單元格內(nèi)容修改列表框中被選中的內(nèi)容

? ? ? ? ? ? ? ? If InStr(t, .List(i)) Then

? ? ? ? ? ? ? ? ? ? .Selected(i) = True

? ? ? ? ? ? ? ? ? ? Else

? ? ? ? ? ? ? ? ? ? .Selected(i) = False

? ? ? ? ? ? ? ? End If

? ? ? ? ? ? Next

? ? ? ? ? ? Reload = False

? ? ? ? ? ? .Top = ActiveCell.Top + ActiveCell.Height '以下語句根據(jù)活動單元格位置顯示列表框

? ? ? ? ? ? .Left = ActiveCell.Left

? ? ? ? ? ? .Width = ActiveCell.Width

? ? ? ? ? ? .Visible = True

? ? ? ? ? ? Else

? ? ? ? ? ? .Visible = False

? ? ? ? End If

? ? End With

End Sub


調(diào)試 >> 編譯

再點(diǎn)擊一次“設(shè)計模式”,讓我們看看效果吧


超棒!nice!完結(jié),撒花~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容