跳至主要内容

如何在Excel中快速在两个列表框之间移动项目?

Author: Sun Last Modified: 2025-07-31

您是否尝试过根据下图所示,将一个列表框中的项目移动到另一个列表框中?这里我将讨论在Excel中进行此操作的方法。

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

在列表框之间移动项目


在列表框之间移动项目

没有内置功能可以帮助您完成这项工作,但我有一个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,并在Caption旁边的文本框中输入>>。参见截图:

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 Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!