将多列列表框中的多个选定项目添加到 Excel 工作表
Add multi selected items in listbox with multi column to an Excel worksheet
我正在尝试将用户表单多列列表框中的所有选定项目添加到我的 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
这段代码有效,但问题是它只保存了我的列表框中最后选择的项目,它没有遍历每个选择的项目。请帮忙
尝试更改为以下代码:
ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(intItem, 0)
ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(intItem, 1)
我正在尝试将用户表单多列列表框中的所有选定项目添加到我的 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
这段代码有效,但问题是它只保存了我的列表框中最后选择的项目,它没有遍历每个选择的项目。请帮忙
尝试更改为以下代码: ActiveCell.Offset(0, 3).Value = Me.ListBox1.List(intItem, 0) ActiveCell.Offset(0, 4).Value = Me.ListBox1.List(intItem, 1)