Excel VBA - 导致错误的listbox.selected属性

时间:2015-07-21 11:30:57

标签: excel vba excel-vba listbox

我在Excel和VBA中开发程序,并且在尝试更改列表框的选定属性时出现运行时错误380.

该属性由Excel电子表格上的列确定,包含True或False值。我已将这些单元格的内容打印到控制台,并可以确认true / false值是否正常工作,但是当我尝试将这些值分配给listbox.selected属性时,我收到错误。

该功能如下,任何帮助或建议都会非常感激。此行发生错误:

ElementListBox.Selected(count - 1)= TaskListSheet.Cells(TaskListCellRef(Task,ref.Row)+ count + 1,TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included))。Value

Public Function LoadTier2SubTaskList(ByVal Task As Single, ByRef ElementListBox As Control)

ElementListBox.Clear
WorklistComboBox.Clear


Dim count As Single
Dim finished As Boolean
Dim TaskListSheet As Worksheet
Set TaskListSheet = TBSheet
finished = False

For count = 1 To 50

Next count

count = 1
Dim TaskString As String

Do While finished = False
TaskString = TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.ElementOfWork)).Text
If TaskString = vbNullString Then
finished = True

ElseIf TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Tier)).Value = 2 Then

ElementListBox.AddItem (TaskString)
ElementListBox.Selected(count - 1) = TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included)).Value
Debug.Print (TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included)).Value)
End If

count = count + 1
Loop

End Function

1 个答案:

答案 0 :(得分:0)

您需要将该值转换为布尔值吗?

ElementListBox.Selected(count - 1) = CBool(TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included)).Value)

让我们开始消除一些潜在的原因!

这有用吗?

ElementListBox.Selected(count - 1) = true

尝试此操作(将代码从count=1替换为Loop)...

count = 1
Dim itemCount as Integer
itemCount = 1
Dim TaskString As String

Do While finished = False
TaskString = TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.ElementOfWork)).Text
If TaskString = vbNullString Then
finished = True

ElseIf TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Tier)).Value = 2 Then

ElementListBox.AddItem (TaskString)
ElementListBox.Selected(itemCount - 1) =    CBool(TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1,  TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included)).Value)
Debug.Print (TaskListSheet.Cells(TaskListCellRef(Task, ref.Row) + count + 1, TaskBreakdownColumnRefs(TaskBreakdownColumnHeaders.Included)).Value)
itemCount = itemCount +1
End If

count = count + 1
Loop

请注意添加itemCount变量以计算listbox

中的项目数