仅检索已检查过vba的复选框

时间:2014-07-10 20:46:11

标签: excel-vba vba excel

你好我有这个循环来检查天气复选框是否已被检查,但是这个循环创建的数组存储了复选框列表的每个复选框值,无论它是否被选中。所以,我不知道如何创建第二个循环,只收集已从数组SelectedItemArray1(i)中检出的复选框。非常感谢您的帮助,这是我迄今为止所做的。

 For i = 0 To Sheet1.ListBox1.ListCount - 1
    If Sheet1.ListBox1.Selected(i) = True Then
        SelectedItemArray1(i) = Sheet1.ListBox1.List(i)
    End If

    MsgBox SelectedItemArray1(i)
 Next

1 个答案:

答案 0 :(得分:1)

试试这个(未经测试的)代码,看看它对您的效果如何:

Dim Msg As String
Dim i As Integer
If ListBox1.ListIndex = -1 Then
    Msg = "Nothing"
Else
    Msg = ""
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            Msg = Msg & ListBox1.List(i) & vbCrLf
        End If
    Next i
End If

如果您的列表框允许多选复选框,那么它就是一种不同的动物。我做了一些谷歌搜索,发现this article,希望能给你一些想法。另外,请查看this article,这似乎更完整。


修改

我认为从我链接的第一篇文章中提供多选代码也可能有所帮助:

Dim i As Long
Dim j As Long
Dim msg As String
Dim arrItems() As String
ReDim arrItems(0 To ListBox1.ColumnCount - 1)
For j = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(j) Then
        For i = 0 To ListBox1.ColumnCount - 1
            arrItems(i) = ListBox1.Column(i, j)
        Next i
        msg = msg & Join(arrItems, ",") & vbCrLf & vbCrLf
    End If
Next j
MsgBox msg