无法设置列表属性。无效的属性值错误

时间:2016-06-26 07:49:07

标签: excel vba excel-vba

我有代码查找"!UKINADMISSIBLE"在专栏" M"的表单。然后它显示所有选定的行"!UKINADMISSIBLE"在列表框(listbox1)中。它运作良好,但如果我删除所有"!UKINADMISSIBLE"从这张表中它给我这个错误(无法设置列表属性,无效的属性值)在这行代码---> Me.ListBox1.List = arrLstBox()--->错误 请有人帮我解决。

[Route("m/api/Group")]
        [HttpGet]
        public IHttpActionResult ListOfGroups()
        {
            try
            {
                var listOfGroups = GroupExecutor.GetListOfGroups();
                return Ok(listOfGroups);
            }
            catch (Exception ex)
            {
                LogClass.Logger.Error(ex.Message, ex);
                return InternalServerError();
            }
        }

2 个答案:

答案 0 :(得分:0)

问题是你的数组是空的。在您的代码中,您应该对其进行测试,如果是这种情况,则只需Clear ListBox

您的代码似乎效率低下。例如,您正在整个文档中搜索您希望在M列中找到的项目并运行搜索两次。为什么不在M列中搜索一次并存储'点击'变量中的行?然后,您可以简单地用这些行填充ListBox数组。您也可以考虑在ListBox事件中仅调整Userform_Initialize列的大小。

您还应该知道,您的大多数声明都是Variants。您必须明确声明每个变量,如Dim a As Integer, b As Integer

这个的骨架代码可能如下所示:

Option Explicit

Private Sub btnIUK_Click()
    Dim v As Variant
    Dim i As Long
    Dim j As Long
    Dim hits As Collection
    Dim hit As Variant
    Dim arrItems() As Variant

    'Read values into an array
    v = ThisWorkbook.Worksheets("Sheet1").UsedRange.Value2

    'Find the target values
    Set hits = New Collection
    For i = 1 To UBound(v, 1)
        If v(i, 13) = "!UKINADMISSIBLE" Then hits.Add i
    Next

    'Populate the listbox array with the hit items
    If hits.Count > 0 Then
        ReDim arrItems(1 To hits.Count, 1 To UBound(v, 2))
        i = 1
        For Each hit In hits
            For j = 1 To 13
                arrItems(i, j) = v(hit, j)
            Next
            i = i + 1
        Next
        Me.ListBox1.List = arrItems
    Else
        'There are not hits so clear the listbox
        Me.ListBox1.Clear
    End If

End Sub

Private Sub UserForm_Initialize()
    With Me.ListBox1
        .ColumnCount = 13
        .ColumnWidths = "60;70;190;40;90;90;70;80;50;60;90;120;5"
    End With

End Sub

答案 1 :(得分:0)

你快到了。 如果列M中至少有一个单元格具有搜索值

,则只需将元素添加到列表框中

此外,如果要添加的数据始终位于A到M列,那么您可以避免计算列并将列表框设置转换为UserForm_Initialize Sub。

像这样:

Private Sub btnIUK_Click()

Dim arrLstBox() As Variant
Dim foundCell As Range
Dim i As Long, j As Long, nCells As Long
Dim firstAddress As String

With ThisWorkbook.Worksheets("MySheet") '<--| always specify the worksheet name
    With .Range("M", .Cells(.Rows.Count, 13).End(xlUp)) '<--| consider column M cells down to its last non empty one
        nCells = WorksheetFunction.CountIf(.Cells, "!UKINADMISSIBLE") '<--| count searched value occurrences in column M
        If nCells > 0 then '<--| If there's at least one occurrence ...
            ReDim arrLstBox(1 To nCells, 1 To 13) '<--| ... ReDim your array...
            Set foundCell = .Find(what:="!UKINADMISSIBLE", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) '<--| ...find first occurrence (it's there for sure!)
            firstAddress = foundCell.Address '<--| ...and store first occurrence address
            Do  '<--| first loop is granted! 
                i = i + 1 '<--| update array row index
                For j = 1 To 13 '<--| fill array row
                   arrLstBox(i, j) = foundCell.Offset(,-13 + j) '<--| use Offset from found cell to sta in its row and loop through columns 1 To 13
                Next
                Set foundCell = .FindNext (foundCell) '<--| look for subsequent occurrence
            Loop While firstAddress <> foundCell.Address '<--| subsequent loops are made till Find() wraps back to the first one
            Me.ListBox1.List = arrLstBox
        End If '<--| fill listbox
    End With 
End With 
MsgBox "Records Found = " & nCells, vb, "Inadmissibles On UK Sectors"

End Sub


Private Sub UserForm_Initialize()
    With Me.ListBox1
        .ColumnCount = 13
        .ColumnWidths = "60;70;190;40;90;90;70;80;50;60;90;120;5"
    End With
End Sub