Find and FindNext in VBA

前端 未结 2 1256
甜味超标
甜味超标 2021-01-16 19:45

I am working on Excel macro. What i need when getting data from another excel sheet, code should first check if there is any other row with the same FundName and if found th

相关标签:
2条回答
  • 2021-01-16 20:06

    Find will use the first cell of Range for the After parameter, if it is not specified, therefore the search is started after B2, and thus the first cell it finds is B6. If the order is important for you then call Find with the last cell provided as After:

        Dim counter As Integer
        counter = 0
    
        With shtData.Range(CCell, DCell)
            Set c = .Find(SearchString, LookIn:=xlValues, LookAt:=xlWhole, After:=DCell)
            If Not c Is Nothing Then
                firstAddress = c.Address
                Do
                    counter = counter + 1
                    Debug.Print "The next match #" & counter & " is " & c.Address
    
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If
        End With
    
    0 讨论(0)
  • 2021-01-16 20:15

    Replace this:

    Set FindRow = shtData.Range(CCell, DCell).Find(What:=SearchString, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
    
    Set NextRow = shtData.Range(CCell, DCell).FindNext(After:=FindRow)
    

    With:

    If WorksheetFunction.CountIf(CCell.EntireColumn, SearchString) > 1 Then
       'Duplicate found, do something here
    Else
       'Unique string, do something here
    End If
    

    Or

    If Evaluate("COUNTIF(" & CCell.EntireColumn.Address & "," & SearchString & ")") > 1 Then
       'Duplicate found, do something here
    Else
       'Unique string, do something here
    End If
    
    0 讨论(0)
提交回复
热议问题