【问题标题】:Add multi selected items in listbox with multi column to an Excel worksheet将具有多列的列表框中的多选项目添加到 Excel 工作表
【发布时间】:2019-09-18 05:39:04
【问题描述】:

我正在尝试将用户窗体上多列列表框中的所有选定项目添加到我的 Excel 工作表中。这些是我目前正在使用的代码:

Dim lrow As Range
Dim wst As Worksheet
Dim lo As ListObject
Dim lr As ListRow

Application.ScreenUpdating = False

Dim xRow As Integer, intItem As Integer

Set wst = Sheets("TRAININGS PROFILE")
wst.Activate
wst.Range("B4").Select

For intItem = 0 To listbox1.ListCount - 1
If listbox1.Selected(intItem) = True Then

Set lo = wst.ListObjects(1)
Set lr = lo.ListRows.Add

ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
ActiveCell.Offset(1, 0).Select
End If
Next intItem

Application.ScreenUpdating = True

此代码有效,但问题是它仅保存我的列表框中的最后一个选定项,它不会循环遍历每个选定项。请帮忙。

【问题讨论】:

    标签: excel vba listbox


    【解决方案1】:

    尝试更改以下代码: ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(intItem, 0) ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(intItem, 1)

    【讨论】:

    • 像魅力一样工作!现在我将对其进行设置,以便每次添加新数据时,它都会在添加之前首先找到最后一行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多