Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用vba在文本框中键入时在listview中搜索_Excel_Vba - Fatal编程技术网

Excel 使用vba在文本框中键入时在listview中搜索

Excel 使用vba在文本框中键入时在listview中搜索,excel,vba,Excel,Vba,我使用了OnKeyUp,所以当用户键入时,它会在列表视图中搜索。我的代码出错了 “参数数目错误或属性分配无效” 下面是我的代码: Private Sub txtEmailGenSearch_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) Dim strText As String Dim i As Long strText = LCase(txtSearch.value)

我使用了OnKeyUp,所以当用户键入时,它会在列表视图中搜索。我的代码出错了

“参数数目错误或属性分配无效”

下面是我的代码:

Private Sub txtEmailGenSearch_KeyUp(ByVal KeyCode As
    MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim strText As String
    Dim i As Long

    strText = LCase(txtSearch.value)
    With MainForm.lstMailGen
        For i = 0 To .ListItems.count - 1
            If LCase(Left(.ListItems(i, 0), Len(strText))) = strText Then 
                Exit For

        Next i

        If i = .ListItems.count Then
            ' No matching item was found, select nothing
            .ListIndex = -1
        Else
            ' A match was found, select it
            .ListIndex = i
        End If
    End With
End Sub

ListView与ListBox完全不同。ListView中的每一行都是一个ListItem。如果ListView有多个列,则每个ListItem将包含ListSubItems。这适用于Microsoft Windows Common Controls 6.0 Listview,5.0的工作原理稍有不同。ListView不会像ListBox具有ListIndex属性那样返回2D数组

建议阅读:

使用
ListItem.Find()
查找匹配的ListItem

在中,要使列表项高亮显示,请确保
HideSelection=False

Listitems第一个索引是1而不是0


如果最后一个项目包含字符串,而不是
如果i=.ListItems.count,则
将跳过选择
如果i>.ListItems.count,则
是正确的方法。如果
For
循环完成,则
i
将额外增加一次。在上述情况下,
i
will=.ListItems.Count+1`如果循环完成。

您的问题是什么?你的问题在哪里?你的代码怎么了?如果您想让我们回答,您需要问一些问题(请参阅)。您在哪一行得到错误?你需要尽可能的具体和详细请注意,您的
If LCase…Then
语句没有
End If
Hi我在
.ListItems(I,0)
部分中一直遇到错误。它表示参数数目错误或属性赋值无效。@RobinScherbatsky我在编辑代码时出错。在任何情况下,
.ListItems
都是一个集合,而不是二维数组。使用
.ListItems(i)
。如果该值在第二列中,则使用“.ListItems(i).ListSubItems(1)。
With MainForm.lstMailGen
    Dim item As ListItem
    Set item = .FindItem(sz:=txtSearch.value, fPartial:=lvwPartial)
    If Not item Is Nothing Then
        item.Selected = True
    End If
End With
MainForm.lstMailGen.HideSelection = False
 For i = 1 To .ListItems.Count 
     If LCase(Left(.ListItems(i), Len(strText))) = strText Then 
        Exit For
 Next i