列表框选择到vba中的其他单元格

时间:2016-03-18 22:54:02

标签: vba excel-vba excel

我在某些VBA代码中遇到了一个小问题,但却很麻烦(我对VBA完全不熟悉)。我创建了一个列表框,链接到一个工作表中的数组。我还创建了一个下拉列表,而一旦用户单击一个选项,列表框中的数组就会发生变化。要添加,此列表框设置为具有多选。

我面临的问题是:我想将列表框设置为一旦用户点击他们选择的每个选项并通过点击"提交"我也创建的按钮,每个选项将填充在其他工作表中的其他特定单元格中。以下是截图:

screenshot of listbox

下面是我研究并放在一起并放入命令按钮代码区的代码。它目前正在做的是点击运行图标,它在列表框中首先选择并仅将第一个选项填充到我定义的单元格中,而不检查列表框中的选项并点击提交按钮。虽然我的愿景似乎有效,但我再次希望它能够填充用户在列表框中选择的内容:

Set sht = Sheets("Results")

Dim I

Dim j

Dim sht as Worksheet

Set sht = Sheets("Results")

j = 1 
For Each i In Me.ListBox21.List
    j = j + 1 
    sht.Cells(5, 1).Value = sht.Cells(5, 1).Value & Chr(10) & i
    sht.Cells(6, 1).Value = sht.Cells(6, 1).Value & Chr(10) & i
    sht.Cells(62, 1).Value = sht.Cells(62, 1).Value & Chr(10) & i
    sht.Cells(63, 1).Value = sht.Cells(63, 1).Value & Chr(10) & i
 Next i

我希望这看起来正确。如果不让我知道,我会截取代码截图。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:0)

我假设你放在同一张纸上

  • 一个名为" ListBox21"的ActiveX ListBox控件用它的" Multiselect"属性设置为" fmMultiSelectMulti"

  • 按钮

然后你的代码与你描述的完全相反,即它更新了"结果"具有所有列表框值的单元格,而不是只有第一个选项。

表示你必须使用Selected(i)对象的ListBox属性来检查它的项目是否在索引" i"已被选中,因此使用List(i)属性

更新单元格

如下

Dim i As Integer
Dim sht As Worksheet

Set sht = Sheets("Results")

With Me.ListBox21
    For i = 0 To .ListCount - 1
        If .Selected(i) Then
            sht.Cells(5, 1).Value = sht.Cells(5, 1).Value & Chr(10) & .List(i)
            sht.Cells(6, 1).Value = sht.Cells(6, 1).Value & Chr(10) & .List(i)
            sht.Cells(62, 1).Value = sht.Cells(62, 1).Value & Chr(10) & .List(i)
            sht.Cells(63, 1).Value = sht.Cells(63, 1).Value & Chr(10) & .List(i)
        End If
    Next i
End With

答案 1 :(得分:0)

当您只想获取所选项目时,代码会变得复杂一些。虽然您只需要迭代.Selected()集合,但您正在使用整个ListBox21.List集合将数据复制到工作表中。由于List集合由ListBox中的所有单元格(所有行和所有列)组成,因此我们也必须遍历列。因此,生成的代码变为:

Dim sht As Worksheet
Dim intListItem As Integer
Dim intColumn As Integer

Set sht = Sheets("Results")

With Me.ListBox21
    For intListItem = 0 To .ListCount - 1
        If .Selected(intListItem) Then
            For intColumn = 0 To .ColumnCount - 1
                sht.Cells(5, 1).Value = sht.Cells(5, 1).Value & Chr(10) & .List(intListItem, intColumn)
                sht.Cells(6, 1).Value = sht.Cells(6, 1).Value & Chr(10) & .List(intListItem, intColumn)
                sht.Cells(62, 1).Value = sht.Cells(62, 1).Value & Chr(10) & .List(intListItem, intColumn)
                sht.Cells(63, 1).Value = sht.Cells(63, 1).Value & Chr(10) & .List(intListItem, intColumn)
            Next intColumn
        End If
    Next intListItem
End With

答案 2 :(得分:0)

非常感谢大家!发布的建议代码实际上有效!我现在的问题是单元格中只有一组列表框选项中的某些选项。例如,该集合将仅发送我检查的四个选项中的两个。是因为为i设置的整数?我现在将命令按钮代码设置为:

With Me.ListBox21

For i = 0 To .ListCount - 1 If .Selected(i) Then sht.Cells(4, 4).Value = sht.Cells(4, 4).Value & Chr(10) & .List(i) End If Next i

For i = 1 To .ListCount - 1 If .Selected(i) Then sht.Cells(4, 7).Value = sht.Cells(4, 7).Value & Chr(10) & .List(i) End If Next i

For i = 2 To .ListCount - 1 If .Selected(i) Then sht.Cells(4, 10).Value = sht.Cells(4, 10).Value & Chr(10) & .List(i) End If Next i

For i = 3 To .ListCount - 1 If .Selected(i) Then sht.Cells(4, 13).Value = sht.Cells(4, 13).Value & Chr(10) & .List(i) End If Next i

End With

我是否需要使用列表框代码定义列表框选项的顺序?请告诉我。

相关问题