2016-01-08 58 views
1

我想根據單元格範圍(匹配)檢查列表框中的項目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 

告訴我「錯誤381:無法讀取列表屬性索引屬性表無效「。我不明白爲什麼,因爲它確實進入了循環,而且它確實按照它應該做的那樣做。缺少什麼來糾正錯誤?

預先感謝您使用

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

當經歷for循環的最後一次迭代都會有訴訟等於列表項的數量

回答

1

嘗試。但是,列表索引從0開始,因此應該有差異1.

例如,如果您有1個列表項,.ListCount方法會給您1,因此for循環將嘗試訪問索引爲0的列表項並列出索引爲1的項目。這會給你一個錯誤,因爲列表框沒有2個項目。

`

+0

這就是拋出錯誤,謝謝 –

+0

歡迎您!我很高興能夠提供幫助。 –