Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

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

Author Sun Last modified

您是否曾經嘗試過根據以下截圖所示,將一個列表框中的項目移動到另一個列表框中?這裡我將介紹如何在 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 助手:以智能執行為基礎,革新數據分析 生成程式碼 創建自訂公式 分析數據並生成圖表 調用 Kutools 增強函數
熱門功能查找、選取項目的背景色或標記重複值刪除空行合併列或單元格且不遺失數據四捨五入(免公式)...
高級 LOOKUP多條件 VLookup多值 VLookup多表查找模糊查找...
高級下拉列表快速創建下拉列表 依賴型下拉列表 多選下拉列表...
列管理器添加指定數量的列移動列切換隱藏列的顯示狀態比較區域及列...
精選功能網格聚焦 設計檢視 增強編輯欄 工作簿及工作表管理器 資源庫(快捷文本) 日期提取器 合併資料 加密/解密儲存格 按列表發送電子郵件 超級篩選 特殊篩選(篩選粗體/傾斜/刪除線...)...
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%,每日可幫您減少數百次鼠標點擊!

所有 Kutools 外掛,一次安裝

Kutools for Office 套裝整合了 Excel、Word、Outlook 和 PowerPoint 的外掛,外加 Office Tab Pro,非常適合需要跨 Office 應用程式協同作業的團隊。

Excel Word Outlook Tabs PowerPoint
  • 全合一套裝 — Excel、Word、Outlook及 PowerPoint 外掛 + Office Tab Pro
  • 一鍵安裝,一份授權 — 幾分鐘完成設置(支援 MSI)
  • 協同運作更順暢 — Office 應用間無縫提升生產力
  • 30 天全功能試用 — 無需註冊、無需信用卡
  • 最超值 — 一次購買,節省單獨外掛費用