计算使用Excel的“查找方法”找到的搜索结果总数

时间:2018-10-25 16:32:18

标签: excel vba excel-vba

是否可以返回使用Excel的find方法找到的匹配总数?如果是这样,那会是什么样?我该如何计算搜索结果的总数?

这是我到目前为止想要建立的:

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 :(得分:2)

  

计算搜索总数

您可以使用CountIF()

还要始终明确限定范围引用直至所需的工作表

最后,请记住Mathieu Guindon的建议

如下:

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

答案 1 :(得分:0)

我在想:

function sortRange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Your Sheet 1');
  var range = sheet.getRange("A7:M100");
  range.sort({column: 2, ascending: true});
 }