excel - 如何在Excel VBA中修复 "Run-time error ' 380'?

标签 excel vba search listbox

我正在 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

最佳答案

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

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 对数组所说的内容。

关于excel - 如何在Excel VBA中修复 "Run-time error ' 380'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56672570/

相关文章:

sql-server - 将日期列从 excel 导入 SSIS - 在 SQL 中获取长日期格式

c# - 从 .NET 中的 VBA 函数访问返回值?

java - 在大型数据集中查找唯一条目的最有效方法

VBA OpenRecordset Producing 参数太少。预期 2. 错误

elasticsearch - Elasticsearch 是否使用以前的搜索频率?

c# - 深度优先搜索确实找到了传教士和食人者问题的解决方案状态

excel - 多个外部工作簿的 Worksheet_Change 事件

Excel-VBA - 如果喜欢*值*,则为 X 轴点着色

asp.net - 无法理解从 HTML 导出 Excel 电子表格的代码

vba - 如果存在值查找,则excel vba查找关键字列表,然后编辑另一个单元格的值