
我在 Excel 中有一張表格,可以在其中從一個單元格的下拉清單中選擇多個項目。
現在,使用我的宏,我必須單獨選擇所有選項,並將它們一一添加到單元格中。
是否可以編寫將複選框添加到下拉列表的宏,以便我可以檢查要添加的項目並且它們將同時添加?
答案1
答案2
好的,我將以下程式碼用於列錶框:
Dim lngItem As Long
Const strSep = ", " 'delimiter
Private Sub UserForm_Initialize()
With ListBox1
.RowSource = "=Sheet1!A2:A12"
For lngItem = 0 To ListBox1.ListCount - 1
.Selected(lngItem) = InStr(1, strSep & ActiveCell.Value & strSep, strSep & .List(lngItem, 0) & strSep)
Next lngItem
End With
End Sub
Private Sub CommandButton1_Click()
Dim strItems As String
With ListBox1
For lngItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lngItem) Then
strItems = strItems & strSep & ListBox1.List(lngItem, 0)
End If
Next lngItem
End With
With ActiveCell
.NumberFormat = "@"
.Value = Replace(strItems, strSep, "", 1, 1)
End With
Unload Me
End Sub
這是用於列選擇的:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 3 Or Target.Row = 1 Then Exit Sub
UserForm1.Show
Cancel = True
End Sub
它效果很好,但我想改變一件事。
目前,雙擊單元格後會顯示列錶框,是否有可能使其只需一鍵單擊?還可以更改它以便列錶框始終顯示在所選單元格下方嗎?