維護好數據,并新增一個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
效果