【问题标题】:How to fix " Run-time error '380' in Excel VBA?如何修复 Excel VBA 中的“运行时错误‘380’?
【发布时间】:2019-09-23 13:18:12
【问题描述】:

我在 Excel VBA 中的列表框中添加了 10 多个列。我不断收到运行时错误“380”-无效的属性值。它可以正常工作,直到列表框中的第 9 列。我在其他任何地方都找不到任何合适的解决方案。有人知道这个问题的解决方法吗?

Private Sub txtSearch_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal 
Shift As Integer)
Dim rng As Range
Set rng = Range("Lookup")
Dim rw
Dim strText As String
strText = LCase(txtSearch.Text)
With ListBox1
.RowSource = ""
.ColumnCount = 12

 For Each rw In rng.Rows
    If InStr(LCase(Cells(rw.Row, 4)), strText) Then
        .AddItem Cells(rw.Row, 1).Value
        .List(ListBox1.ListCount - 1, 1) = Cells(rw.Row, 2).Value
        .List(ListBox1.ListCount - 1, 2) = Cells(rw.Row, 3).Value
        .List(ListBox1.ListCount - 1, 3) = Cells(rw.Row, 4).Value
        .List(ListBox1.ListCount - 1, 4) = Cells(rw.Row, 5).Value
        .List(ListBox1.ListCount - 1, 5) = Cells(rw.Row, 6).Value
        .List(ListBox1.ListCount - 1, 6) = Cells(rw.Row, 7).Value
        .List(ListBox1.ListCount - 1, 7) = Cells(rw.Row, 8).Value
        .List(ListBox1.ListCount - 1, 8) = Cells(rw.Row, 9).Value
        .List(ListBox1.ListCount - 1, 9) = Cells(rw.Row, 10).Value
        .List(ListBox1.ListCount - 1, 10) = Cells(rw.Row, 11).Value
        .List(ListBox1.ListCount - 1, 11) = Cells(rw.Row, 12).Value
        .List(ListBox1.ListCount - 1, 12) = Cells(rw.Row, 13).Value           
    End If
Next    

End With
End Sub

【问题讨论】:

  • 将值添加到数组 (arr) 然后设置 .List = arr。没有进一步挖掘,我猜这是您列表中的计数问题。
  • 谢谢马修。你能帮我把你的想法和 A Cohen 的提议合并一下吗?

标签: excel vba search listbox


【解决方案1】:

我不知道这是否会解决所有问题,但它肯定会清理一下。另外,我不确定您从哪个工作表中提取Cells(rw.Row, 2).value。但它们可能与它为什么中途停止有关。另外,要稍微清理一下,请尝试额外的For Statement

Private Sub txtSearch_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    Dim rng As Range: Set rng = Range("Lookup")
    Dim rw
    Dim strText As String: strText = LCase(txtSearch.Text)

    With ListBox1
        .RowSource = ""
        .ColumnCount = 21

        For Each rw In rng.Rows
            If InStr(LCase(Cells(rw.Row, 4)), strText) Then
                .AddItem Cells(rw.Row, 1).Value
                For x = 1 To 12  '''Change Worksheet to your Worksheet name
                    .List(ListBox1.ListCount - 1, x) = Worksheets("Sample").Cells(rw.Row, x + 1).Value2
                Next x
            End If
        Next

    End With

End Sub

如果这没有帮助,试试@Cyril 所说的数组。

【讨论】:

  • 谢谢。您的代码有助于清理它。但我仍然遇到错误。你能帮我把你的和@Cyril 的提议合并吗?
  • @FaridFarmani 您是否会收到错误 380 或其他错误?
  • 我对如何合并@Cyril 的想法有点困惑
  • Im also working on it. If I can figure it out Ill 在此处发布我的解决方案。
  • @ACohen 我为此添加了我的,所以希望你能理解我所做的......解释在帖子的底部。
【解决方案2】:

刚刚回复你...有点长,但这里是一般的想法...

这一切都在 ActiveX 控件的代码中:

Option Explicit

Sub ListBox1_Click()
    Dim rw As Range, strtext As String
    Dim arr As Variant, ai As Long, aj As Long 
    Dim brr As Variant, bi As Long, bj As Long
    strtext = "a" 'I used this when i did my testing
    ReDim arr(11, 0)
    For Each rw In Range("rng")
        If InStr(LCase(rw.Value), strtext) Then
            aj = findaj(arr)
            If Not IsEmpty(arr(1, aj)) Then
                aj = aj + 1
                ReDim Preserve arr(11, aj)
            End If
            For ai = 1 To 11
                arr(ai, aj) = Cells(rw.Row, ai + 1).Value
            Next ai
        End If
    Next rw
    ReDim brr(aj, 11)
    For bi = 0 To aj
        For bj = 1 To 11
            brr(bi, bj) = arr(bj, bi)
        Next bj
    Next bi
    ListBox1.ColumnCount = 11
    ListBox1.List = brr
End Sub

Private Function findaj(ByVal brr As Variant)
    Dim j As Long, meow As String
    j = 0
    Do While True
        On Error GoTo toll
        j = j + 1
        meow = brr(1, j)
    Loop
toll:
    findaj = j - 1
End Function

所以这里发生了很多事情......我使用了两个单独的数组,因为重新调整数组在 VBA 中是如何工作的。您可以更新数组的第二个元素,因此 arr(ai,aj) 只能在我 redim preserve 向我的数组添加新行时更新 aj

所以我们创建了一个数组 (arr),它根据 VBA 的限制捕获数据。在该数组中,我们使用了一个函数findaj,该函数有意捕获错误以确定arr 中适当的最后一个(我用斜体表示列的使用,因为情况并非如此,但考虑到它在空间上是有意义的)。

然后您将数组arr 转换为brr 以适当的列/行顺序。

然后,您将自己的 .list = brr 设为。

【讨论】:

  • 感谢您的代码和描述。我尝试了代码,但在“ListBox1.List = brr”上不断收到“运行时错误'70'-权限被拒绝'。在我输入整个搜索词的第一个字母后,也需要一段时间。您是否随时收到此错误?
  • @FaridFarmani 我没有;我确保在我的参考文献中打开了 MSForms 2.0 库,但我认为这不会拒绝许可。你的列表框被锁定了吗?
  • @Cyril - 这远远超出了我的专业知识,如果我不关注,请见谅。
  • @Cyril 我检查了 MSForms 和列表框,一切正常。不知道这里有什么问题。我们有没有机会使用 Autofilter 方法完成这项工作?
  • @FaridFarmani 请参阅stackoverflow.com/a/43350502/3233363 这可能是在同一伪装下,您需要ListBox1.RemoveItem。我很抱歉错过了这一点;我的 ListBox 知识来自 userForms,它是在 Initialize 时生成的,而不是被重写的。这将导致删除项目的解决方案:stackoverflow.com/a/2998231/3233363(在标题中说组合框,但答案是关于列表框)
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2019-03-01
  • 2021-09-11
相关资源
最近更新 更多