【问题标题】:listbox selections to other cells in vbavba中其他单元格的列表框选择
【发布时间】:2016-03-18 22:54:01
【问题描述】:

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

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

下面是我研究整理并放入命令按钮代码区的代码。它当前所做的是在我点击运行图标时,它在列表框中采用第一个选择并将仅该第一个选择填充到我定义的单元格中,而无需我检查列表框中的选择并点击提交按钮。虽然我的设想似乎奏效了,但我再次希望它填充用户在列表框中选择的内容:

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

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

【问题讨论】:

  • 代码中似乎没有使用j(至少这部分代码)。它的目的是什么?

标签: vba excel


【解决方案1】:

我假设在您放置的同一张表中

  • 名为“ListBox21”的 ActiveX ListBox 控件,其“Multiselect”属性设置为“fmMultiSelectMulti”

  • 一个按钮

然后您的代码与您描述的完全相反,即它使用所有列表框值更新“结果”单元格,而不仅仅是第一个选择。

这表示您必须使用ListBox 对象的Selected(i) 属性来检查其索引“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

【讨论】:

  • 您忘记了解决方案中的所有列和附加值。问题中使用的List 集合不仅包含第一列(或绑定),还包含所有其他列。因此,您需要复制 ListBox 的所有行和所有列。
  • Q 没有提到多列列表框。还有截图。所以我选择了单列解决方案。如果它是一个多列列表框,那么它可以像您一样轻松扩展。欢呼
  • 非常感谢大家!
【解决方案2】:

当您只想获取所选项目时,代码会变得有点复杂。虽然您只需要迭代 .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

【讨论】:

    【解决方案3】:

    非常感谢大家!发布的建议代码实际上有效!我现在的问题是单元格仅填充了列表框选项集中的某些选项。例如,该集合只会发送我勾选的四个选项中的两个。是因为为 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

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

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多