ListBox上的运行时错误'380'[无法设置Selected属性。无效的属性值。]

时间:2014-09-19 18:52:36

标签: excel excel-vba listbox vba

我已经回顾了10个报告的问题,但错误代码类似,但没有一个符合我的情况。 我有一个使用.List属性填充的ListBox ... ListBox的行为与预期的一样,除了(按顺序): 1.我过滤掉未选择的项目并使用过滤的项目重置.List属性(没有问题) 2.我试图将所有项目“标记”为选中(我在设置第一项的.Selected属性后立即收到错误。)

这是代码(我尝试了很多不同的变体,但这是“最干净的”):

Set rngTarget = rngTarget.Range(rngTarget.Cells(2, 1), rngTarget.Cells(rngTarget.Rows.Count, lbSelection.ColumnCount))
lbSelection.Clear
lbSelection.List = rngTarget.Cells.Value
lItemCnt = lbSelection.ListCount - 1
For lItemNdx = 0 To lItemCnt
    lbSelection.Selected(lItemNdx) = True
Next lItemNdx

首次遇到lbSelection.Selected(lItemNdx) = True时发生错误。所有索引都是有效的,数据集/范围对所有具有有效数据的行/列都有效,并且列表中只有5列(即,小于10的最大值)。我还尝试首先将数据集(范围)分配给变量,然后将变量分配给.List属性,但这没有什么区别。在清除列表之前,我还尝试“取消标记”所有项目。有趣的是,在遇到错误的调试器中,.ListCount属性显示正确的行数。但是,GUI显示一个空白列表。

非常感谢任何帮助。

P.S。使用.AddItem方法“手动”填充ListBox对于我的目的来说不是一个实用的选项(也不是使用.RowSource属性)。

环境:Excel 2010,标准。 Windows 7专业版。

包含更全面的审核代码(数据未包含但由457行组成[第一行是标题而不是列表框的一部分]和5列 - 所有文本)。要复制问题,只需取消选择列表中的第一项即可。

调用userform的按钮:

`Sub LoadSampleForm()
    frmTestLB.InitControls
End Sub`

用户形式代码:

`选项明确     私有bIgnorEvents为布尔

Public Sub InitControls()
Dim lItemNdx As Long
Dim lItemCnt As Long
    bIgnorEvents = True
    lbSelection.Clear
    lbSelection.ColumnCount = 5
    lbSelection.ColumnWidths = "45 pt;155 pt;70 pt;70 pt;70 pt"
    lbSelection.List = Worksheets("Sample").Range(Worksheets("Sample").Cells(2, 1), Worksheets("Sample").Cells(457, 5)).Value

    lItemCnt = lbSelection.ListCount - 1
    For lItemNdx = 0 To lItemCnt
        lbSelection.Selected(lItemNdx) = True
    Next lItemNdx
    bIgnorEvents = False
    Me.Show
End Sub

Private Sub lbSelection_Change()
Dim bAppAlert As Boolean
Dim lItemCnt As Long
Dim lItemNdx As Long
Dim lKeyColNdx As Long
Dim rngTarget As Range          'Temporary range for items to be searched
Dim wsTemp As Worksheet         'Temporary worksheet used for target range and applying Lookup function
    If (Not bIgnorEvents) Then
        bIgnorEvents = True

        Set wsTemp = ThisWorkbook.Worksheets.Add
        lKeyColNdx = 1
        lItemCnt = lbSelection.ListCount + 1
        wsTemp.Range(Cells(2, 1), Cells(lItemCnt, lbSelection.ColumnCount)).Value = lbSelection.List
        Set rngTarget = wsTemp.Range(Cells(1, 1), Cells(lItemCnt, lbSelection.ColumnCount + 1))
        rngTarget.Cells(1, lbSelection.ColumnCount + 1).Value = "Selected"

        For lItemNdx = 2 To lItemCnt
            If (lbSelection.Selected(lItemNdx - 2)) Then
                rngTarget.Cells(lItemNdx, lbSelection.ColumnCount + 1).Value = 1
            Else
                rngTarget.Cells(lItemNdx, lbSelection.ColumnCount + 1).Value = 0
            End If
        Next lItemNdx

        rngTarget.AutoFilter Field:=lbSelection.ColumnCount + 1, Criteria1:="=0"
        rngTarget.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        wsTemp.AutoFilterMode = False

        Set rngTarget = rngTarget.Range(rngTarget.Cells(2, 1), rngTarget.Cells(rngTarget.Rows.Count, lbSelection.ColumnCount))
        lbSelection.Clear
        lbSelection.List = rngTarget.Cells.Value

        lItemCnt = lbSelection.ListCount - 1
        For lItemNdx = 0 To lItemCnt
            lbSelection.Selected(lItemNdx) = True
        Next lItemNdx

        'Clean-up:  Remove temp sheet without prompts
        bAppAlert = Application.DisplayAlerts   'Current setting
        Application.DisplayAlerts = False       'Supress DisplayAlerts
        wsTemp.Delete                           'Delete temp sheet
        Application.DisplayAlerts = bAppAlert   'Restore original DisplayAlerts setting
        Set rngTarget = Nothing
        Set wsTemp = Nothing

        bIgnorEvents = False
    End If
End Sub`

1 个答案:

答案 0 :(得分:0)

尝试更改行:

lbSelection.List = rngTarget.Cells.Value

要:

lbSelection.List = rngTarget