维护好数据,并新增一个activeX列表框插件
Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Count > 1 Then Exit SubIf Target.Row >= 2 And Target.Row <= 10 And Target.Column = 2 Then '选择操作范围With ListBox1.MultiSelect = 1 '多选模式.ListStyle = 1.List = ActiveSheet.Range("F1:F7").Value.Top = Target.Top.Left = Target.Left + Target.Width.Height = Target.Height * 5.Width = 90.Visible = TrueEnd WithElseListBox1.ClearListBox1.Visible = FalseEnd If
End Sub' 双击切换选中状态
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)Dim clickedIndex As IntegerclickedIndex = ListBox1.ListIndex ' 获取双击的选项索引If clickedIndex >= 0 Then ' 确保双击的是有效选项' 切换选中状态ListBox1.Selected(clickedIndex) = Not ListBox1.Selected(clickedIndex)End If
End Sub' 回车键录入 Excel 单元格
Private Sub ListBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)If KeyCode = vbKeyReturn ThenDim selectedItems As StringDim i As IntegerselectedItems = ""'遍历选中的项目For i = 0 To ListBox1.ListCount - 1If ListBox1.Selected(i) ThenIf selectedItems = "" ThenselectedItems = ListBox1.List(i)ElseselectedItems = selectedItems & ", " & ListBox1.List(i)End IfEnd IfNext iActiveCell.Value = selectedItemsEnd If
End Sub
效果