我无法检查多列用户窗体ListBox中的项目。第lstPreConditionLogic.Selected(lstPreConditionLogic.ListCount) = True
行产生以下错误:
运行时错误'380':无法设置Selected属性。属性值无效
我尝试将值设置为-1
,checked
,但是没有用。
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
答案 0 :(得分:2)
您必须从-1
中减去.ListCount
…
lstPreConditionLogic.Selected(lstPreConditionLogic.ListCount - 1) = True
由于列表索引统计从0
开始计数,例如,如果ListCount = 5
,则这5个条目的索引为0
,1
,2
, 3
,4
,这意味着最后一个有效索引是.ListCount - 1
。