无法在Excel VBA UserForm中设置多列ListBox的selected属性

时间:2019-01-16 07:27:52

标签: excel vba multiple-columns userform checkedlistbox

我无法检查多列用户窗体ListBox中的项目。第lstPreConditionLogic.Selected(lstPreConditionLogic.ListCount) = True行产生以下错误:

  

运行时错误'380':无法设置Selected属性。属性值无效

我尝试将值设置为-1checked,但是没有用。

VBA代码

Private Function FillPreConditionLogicList()
    Dim varTemp             As Variant
    Dim intLoop             As Integer
    Dim strExpression       As String
    Dim PreConditionLogic   As clsPreConditionLogic

    With lstPreConditionLogic
        .ColumnCount = 3
        .ListStyle = fmListStyleOption
        .MultiSelect = fmMultiSelectMulti
        strExpression = TrimBlank(shtExpressionEditor.Range("rngExpText").Offset(, 1).Value)
        For intLoop = 0 To mDicPreConditionLogic.Count - 1
            Set PreConditionLogic = mDicPreConditionLogic.Items(intLoop)
            .AddItem
            .List(intLoop, 0) = PreConditionLogic.Name
            .List(intLoop, 1) = PreConditionLogic.StartEnclosure
            .List(intLoop, 2) = PreConditionLogic.EndEnclosure
            varTemp = GetEnclosedString(strExpression, PreConditionLogic.StartEnclosure, PreConditionLogic.EndEnclosure)
            If varTemp <> "" Then
                lstPreConditionLogic.Selected(lstPreConditionLogic.ListCount) = True '//Gives Error
                strExpression = varTemp
            End If
        Next
    End With
End Function

1 个答案:

答案 0 :(得分:2)

您必须从-1中减去.ListCount

lstPreConditionLogic.Selected(lstPreConditionLogic.ListCount - 1) = True

由于列表索引统计从0开始计数,例如,如果ListCount = 5,则这5个条目的索引为01234,这意味着最后一个有效索引是.ListCount - 1