VBA将列表框对象的值与范围匹配

我正在尝试根据单元格范围(匹配)检查列表框中的项目

这是我迄今为止所做的。

Dim j As Integer Dim lItem As Long Dim rowx as Long rowx = 12 j = 0 For lItem = 0 To Worksheets("Bordereau Prep").ListBoxPlot.ListCount If Worksheets("Bordereau Prep").ListBoxPlot.List(lItem) = Worksheets("Liste").Cells(rowx, 40 + j) Then Worksheets("Bordereau Prep").ListBoxPlot.Selected(lItem) = True j = j + 1 End If Next lItem 

这就是我想要的,检查在range_pr_el列表中的项目,但它会抛出一个错误:

 If Worksheets("Bordereau Prep").ListBoxPlot.List(lItem) = Worksheets("Liste").Cells(rowx, 40 + j) Then 

告诉我“Error 381:Impossible to List List property。Index of the property table not valid”。 我不明白为什么,因为它进入了循环,而且它确实按照预期行事。 缺less什么来纠正错误?

先谢谢你

尝试使用

 For lItem = 0 To Worksheets("Bordereau Prep").ListBoxPlot.ListCount - 1 

当通过for循环时,最后一次迭代将有lItem等于列表项的数量。 但是,列表索引从0开始,所以应该有1的差异。

例如,如果你有1个列表项,.ListCount方法会给你1,所以for循环将尝试访问索引为0的列表项和索引为1的列表项。这会给你一个错误,因为列表框没有2项。

`