Skip to main content

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

Author: Sun Last Modified: 2025-08-06

您是否曾經嘗試過根據以下截圖所示,將一個列表框中的項目移動到另一個列表框中?這裡我將介紹如何在 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

最佳 Office 辦公效率工具

🤖 Kutools AI Aide:徹底革新數據分析,基於智能執行|生成程式碼|創建自訂公式|分析數據並生成圖表|調用 Kutools Functions
熱門功能查找、選取項目的背景色或標記重複值 | 刪除空行 | 合併列或單元格且不丟失資料 | 四捨五入...
高級 LOOKUP多條件查找|多值查找|多表查找|模糊查找...
高級下拉列表快速創建下拉列表 | 關聯下拉列表 | 多選下拉列表 ...
列管理器添加指定數量的列 | 移動列 | 切換隱藏列的可見狀態 | 區域與列比較 ...
精選功能網格聚焦 | 設計檢視 | 增強編輯欄 | 工作簿及工作表管理器 | 資源庫 (自動文本) | 日期提取器 | 合併資料 | 加密/解密儲存格 | 按清單發送電子郵件 | 超級篩選 | 特殊篩選(篩選粗體/傾斜/刪除線 ...)...
前15 大工具集12 款文本工具添加文本刪除特定字符,...)|50+ 種圖表 類型甘特圖,...)|40+ 實用 公式基於生日計算年齡,...)|19 款插入工具插入QR码按路徑插入圖片,...)|12 款轉換工具金額轉大寫匯率轉換,...)|7 款合併和分割工具高級合併行分割儲存格,...)| ...以及更多
使用 Kutools 支援你的語言——支援英語、西班牙語、德語、法語、中文及40 多種語言!

利用 Kutools for Excel 大幅提升你的 Excel 技能,感受前所未有的高效體驗。 Kutools for Excel 提供超過300 項高級功能,助你提升效率並保存時間。 點此查看你最需要的功能...


Office Tab 為 Office 帶來標籤式介面,讓你的工作更加輕鬆

  • 啟用 Word、Excel、PowerPoint 的標籤式編輯和閱讀功能
  • 在同一個視窗的標籤中打開和創建多個文件,而不是在新窗口中分開開啟。
  • 可提升你50% 的工作效率,每天為你大量減少滑鼠點擊次數!