跳至主要内容

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

在列表框之间移动项目


在列表框之间移动项目

没有内置功能可以帮助您完成这项工作,但我有一个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函数……
热门功能:查找、选中项的背景色或标记重复项 | 删除空行 | 合并列或单元格且不丢失数据 | 四舍五入……
高级LOOKUP多条件VLookup|多值VLookup|多表查找|模糊查找……
高级下拉列表快速创建下拉列表 |依赖下拉列表 | 多选下拉列表……
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域与列……
特色功能网格聚焦 |设计视图 | 增强编辑栏 | 工作簿及工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按名单发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/倾斜/删除线等)……
15大工具集12项 文本工具添加文本删除特定字符等)|50+种 图表 类型甘特图等)|40+实用 公式基于生日计算年龄等)|19项 插入工具插入二维码从路径插入图片等)|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天全功能试用——无需注册,无需信用卡
  • 超高性价比——比单独购买更实惠