-1
Private Sub SearchGuess_Click()
    With ThisWorkbook.Worksheets("WP").Range("WP")
        Set Rng = .Find(What:=TextBox1.Value, _
                   LookIn:=xlValues, _
                   LookAt:=xlContains, _
                   SearchOrder:=xlByRows, _
                   SearchDirection:=xlPrevious)
        If Not Rng Is Nothing Then
            rowNumber = Rng.Row
        Else
            rowNumber = 0
        End If
    End With
    MsgBox ThisWorkbook.Worksheets("WP").Cells(rowNumber, 1).Value
End Sub

我希望用户搜索或猜测 Excel 数据库中的字符串,每当他们键入包含数据库数据的字符串时,它就会打印到列表框中,然后他们可以选择、继续和编辑。

任何想法如何做到这一点,上面是我唯一能做的。

4

1 回答 1

1

使用 range.find 循环填充数组,然后将列表框的 .List 属性设置为您创建的数组:

Private Sub SearchGuess_Click()

    Dim rngFound As Range
    Dim arrList() As Variant
    Dim arrIndex As Long
    Dim strFirst As String

    Me.ListBox1.Clear
    With ThisWorkbook.Worksheets("WP").Range("WP")
        Set rngFound = .Find(Me.TextBox1.Text, .Cells(.Cells.Count), xlValues, xlPart)
        If Not rngFound Is Nothing Then
            ReDim arrList(1 To WorksheetFunction.CountIf(.Cells, "*" & Me.TextBox1.Text & "*"))
            strFirst = rngFound.Address
            Do
                arrIndex = arrIndex + 1
                arrList(arrIndex) = .Parent.Cells(rngFound.Row, "A").Text
                Set rngFound = .Find(Me.TextBox1.Text, rngFound, xlValues, xlPart)
            Loop While rngFound.Address <> strFirst
            Me.ListBox1.List = arrList
        Else
            MsgBox "No matches found for [" & Me.TextBox1.Text & "].", , "No Results"
        End If
    End With

End Sub
于 2013-09-04T16:22:38.023 回答