.FindNext failing after a .Find function (excel vba)

前端 未结 2 556
旧时难觅i
旧时难觅i 2020-12-21 08:46

I am trying to use .Find and .FindNext to search through a single column of data. I first need to find the first cell containing the value \"Total\

2条回答
  •  [愿得一人]
    2020-12-21 09:23

    Would this help?

    Topic: .Find and .FindNext In Excel VBA

    Link: http://www.siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/

    Extract From the link:

    Sub Sample()
        Dim oRange As Range, aCell As Range, bCell As Range
        Dim ws As Worksheet
        Dim ExitLoop As Boolean
        Dim SearchString As String, FoundAt As String
        On Error GoTo Err
        Set ws = Worksheets("Sheet3")
        Set oRange = ws.Columns(1)
    
        SearchString = "2"
        Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If Not aCell Is Nothing Then
            Set bCell = aCell
            FoundAt = aCell.Address
            Do While ExitLoop = False
                Set aCell = oRange.FindNext(After:=aCell)
    
                If Not aCell Is Nothing Then
                    If aCell.Address = bCell.Address Then Exit Do
                    FoundAt = FoundAt & ", " & aCell.Address
                Else
                    ExitLoop = True
                End If
            Loop
        Else
            MsgBox SearchString & " not Found"
        End If
        MsgBox "The Search String has been found at these locations: " & FoundAt
        Exit Sub
    Err:
        MsgBox Err.Description
    End Sub
    

提交回复
热议问题