跳至主要内容

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

Author: Sun Last Modified: 2025-05-07

您是否尝试过根据下图所示,将一个列表框中的项目移动到另一个列表框中?这里我将讨论在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

最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!