Подсчет общего количества найденных результатов поиска с помощью метода поиска Excel - PullRequest
0 голосов
/ 25 октября 2018

Можно ли вернуть общее количество найденных совпадений, используя метод поиска Excel?Если да, то как бы это выглядело или как бы я подсчитал общее количество результатов поиска?

Вот что у меня есть, на что я хотел бы опираться:

Private Sub btnSearch_Click()

    With Sheet1
        Set foundCell = .Cells.Find(What:="B-32", After:=.Cells(1, 1), _
        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With

    If Not foundCell Is Nothing Then
            MsgBox ("""Bingo"" found in row " & foundCell.Row)
            UserForm1.location.Text = Cells(foundCell.Row, 3).Value
            UserForm1.office.Value = Cells(foundCell.Row, 2).Value
            UserForm1.floor.Value = Cells(foundCell.Row, 1).Value
            UserForm1.status.Value = Cells(foundCell.Row, 4).Value
            UserForm1.telephone.Value = Cells(foundCell.Row, 5).Value
            UserForm1.mobile.Value = Cells(foundCell.Row, 6).Value
            UserForm1.owner.Value = Cells(foundCell.Row, 7).Value
            UserForm1.notes.Value = Cells(foundCell.Row, 8).Value
    Else
            MsgBox ("Bingo not found")
    End If

End Sub

Ответы [ 2 ]

0 голосов
/ 25 октября 2018

Подсчет общего количества запросов

, которые вы также можете использовать CountIF()

, всегда явно указывать ссылки на диапазон до требуемого листа

наконец, следуйте совету Матье Гиндона

следующим образом:

With Sheet1
    Set foundCell = .Cells.Find(What:="B-32", After:=.Cells(1, 1), _
    LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

    If Not foundCell Is Nothing Then
        MsgBox ("""Bingo"" found " & WorksheetFunction.CountIf(.Cells, "*B-32*") & " times")

        MsgBox ("first ""Bingo"" found in row " & foundCell.Row)

        Me.Location.Text = .Cells(foundCell.Row, 3).Value
        Me.Office.Value = .Cells(foundCell.Row, 2).Value
        Me.Floor.Value = .Cells(foundCell.Row, 1).Value
        Me.Status.Value = .Cells(foundCell.Row, 4).Value
        Me.telephone.Value = .Cells(foundCell.Row, 5).Value
        Me.mobile.Value = .Cells(foundCell.Row, 6).Value
        Me.owner.Value = .Cells(foundCell.Row, 7).Value
        Me.Notes.Value = .Cells(foundCell.Row, 8).Value
    Else
        MsgBox ("Bingo not found")
    End If
End With
0 голосов
/ 25 октября 2018

Я думал:

Option Explicit

'Global Variables
Dim foundCell


Private Sub btnSearch_Click()

Dim Str
Dim FirstAddr As String

Str = "B-32"


    With Sheet1
        Set foundCell = .Cells.Find(What:=Str, After:=.Cells(1, 1), _
                        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With


    If Not foundCell Is Nothing Then

        MsgBox ("""Bingo"" found in row " & foundCell.Row)

        UserForm1.location.Text = Cells(foundCell.Row, 3).Value
        UserForm1.office.Value = Cells(foundCell.Row, 2).Value
        UserForm1.floor.Value = Cells(foundCell.Row, 1).Value
        UserForm1.status.Value = Cells(foundCell.Row, 4).Value
        UserForm1.telephone.Value = Cells(foundCell.Row, 5).Value
        UserForm1.mobile.Value = Cells(foundCell.Row, 6).Value
        UserForm1.owner.Value = Cells(foundCell.Row, 7).Value
        UserForm1.notes.Value = Cells(foundCell.Row, 8).Value

        FirstAddr = foundCell.Address

    Else
            MsgBox ("Bingo not found")
    End If

    Dim i As Integer

    Do Until foundCell Is Nothing

        Set foundCell = Sheet1.Cells.FindNext(After:=foundCell)

        i = i + 1

        If foundCell.Address = FirstAddr Then Exit Do
    Loop

    MsgBox (i)

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...