Skip to main content

如何快速在 Excel 的兩個列表框之間移動項目?

Author: Sun Last Modified: 2025-05-12

您是否曾經嘗試過根據以下截圖所示,將一個列表框中的項目移動到另一個列表框中?這裡我將介紹如何在 Excel 中進行此操作。

a screenshot showing the listboxes before moving items a screenshot of an arrow a screenshot showing the listboxes after moving items

在列表框之間移動項目


在列表框之間移動項目

Excel 沒有內建函數可以幫助您完成這個任務,但我有一段 VBA 代碼可以幫上忙。

1. 首先,您需要在名為 Admin_Lists 的新工作表中創建一個數據列表,這些數據將作為列表框中的項目顯示。

a screenshot of the source data

2. 然後選擇這些數據並前往名稱框,將它們命名為 ItemList。請參閱截圖:

a screenshot of naming the source data in the Name box

3. 接著,在包含這兩個列表框的工作表中,點擊 開發工具 > 插入 > 列表框(Active X 控制項),然後繪製兩個列表框。請參閱截圖:

a screenshot of selecting the List Box control under the Developer tab a screenshot of the right arrow a screenshot showing two created list boxes

如果您的功能區中未顯示「開發工具」選項卡,該如何在 Excel 2007/2010/2013 功能區中顯示或啟用「開發工具」選項卡?本文將告訴您如何顯示它。

4. 然後點擊 開發工具 > 插入 > 命令按鈕(Active X 控制項),並在兩個列表框之間繪製四個按鈕。請參閱截圖:

a screenshot of selecting command button control a screenshot of a right arrow 1 a screenshot showing the created command buttons

現在重新命名這四個命令按鈕的新名稱。

5. 選擇第一個命令按鈕,點擊 屬性,在屬性面板中,給它命名為 BTN_moveAllRight,並在標題旁的文本框中輸入 >>。請參閱截圖:

a screenshot showing how to changing the properties of the command button

6. 重複步驟 5,將最後三個命令按鈕重新命名為以下名稱,並在標題中輸入不同的箭頭符號。請參閱截圖:

BTN_MoveSelectedRight

BTN_moveAllLeft

BTN_MoveSelectedLeft

a screenshot of the second command button after changing the properties a screenshot of the third command button after changing the properties a screenshot of the fourth command button after changing the properties

7. 右鍵單擊包含列表框和命令按鈕的工作表名稱,並從上下文菜單中選擇 檢視程式碼。請參閱截圖:

a screenshot of opening the VBA code editor

8. 複製並將以下宏代碼粘貼到模組腳本中,然後保存代碼並關閉 Microsoft Visual Basic for Applications 窗口。請參閱截圖:

VBA:在兩個列表框之間移動項目

Private Sub Worksheet_Activate()
'UpdatebyExtendoffice20171117
    Dim xCell As Range
    Dim xRg As Range
    Set xRg = Sheets("Admin_Lists").Range("ItemList")
    Me.ListBox1.Clear
    Me.ListBox2.Clear
    With Me.ListBox1
        .LinkedCell = ""
        .ListFillRange = ""
        For Each xCell In xRg
            If xCell <> "" Then
                .AddItem xCell.Value
            End If
        Next xCell
    End With
    Me.ListBox1.MultiSelect = fmMultiSelectMulti
    Me.ListBox2.MultiSelect = fmMultiSelectMulti
End Sub

Private Sub BTN_MoveSelectedLeft_Click()
    Call moveSigle(Me.ListBox2, Me.ListBox1)
End Sub

Private Sub BTN_MoveSelectedRight_Click()
    Call moveSigle(Me.ListBox1, Me.ListBox2)
End Sub

Private Sub BTN_moveAllLeft_Click()
    Call moveAll(Me.ListBox2, Me.ListBox1)
End Sub

Private Sub BTN_moveAllRight_Click()
    Call moveAll(Me.ListBox1, Me.ListBox2)
End Sub

Sub moveAll(xListBox1 As Object, xListBox2 As Object)
    Dim I As Long
    For I = 0 To xListBox1.ListCount - 1
        xListBox2.AddItem xListBox1.List(I)
    Next I
    xListBox1.Clear
End Sub

Sub moveSigle(xListBox1 As Object, xListBox2 As Object)
    Dim I As Long
    For I = 0 To xListBox1.ListCount - 1
        If I = xListBox1.ListCount Then Exit Sub
        If xListBox1.Selected(I) = True Then
            xListBox2.AddItem xListBox1.List(I)
            xListBox1.RemoveItem I
            I = I - 1
        End If
    Next
End Sub

a screenshot showing how to use the VBA code

9. 然後轉到另一個工作表,再返回到包含列表框的工作表,現在您可以看到列表數據已經列在第一個列表框中。點擊命令按鈕以在兩個列表框之間移動項目。

a screenshot showing the source data in one list box after running the VBA code

移動選定項目

a screenshot of moving items from one list box to another one by one a screenshot of the arrow a screenshot showing two items have been moved to the right list box

全部移動

a screenshot of marking which command button can be used to move all items from one list to another at the same time a screenshot of arrow 2 a screenshot showing all items are moved from one list box to another at the same time

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!