开发者

Find method keeps on missing the search string in an excel's workbook using VBA?

I am searching for text/strings in an excel sheet provided by the customer. The problem is that sometimes the excel Find function works and sometimes it does not. For eg if the excel sheet looks like below:

PartID Description Reference  
1234    abc         R3
4567    def         C34
4512    ghi         R2

Lets say i searched for R2 and I found it, but when i search for R3 it just won't hit it. It will find anything after R2 that looks like R3 like R33, R31 etc. Looks like it starts checking from its last position, which was R2's position. Below is my function:

Sub addFeedernoToFile(PARTS As Integer, ByRef counter As Integer, fileptrsq As String, ws_sq As Worksheet, tempList() As String)

    Dim i As Integer, k As Integer, found As Integer
    Dim LastAddress As String

    Dim xlSearchWithin1 As XlSearchWithin

    Set Search_Range = Columns("C")

        For i = 1 To PARTS
            searchstring = tempList(counter)

            With Search_Range
                Set c = ws_sq.Cells.Find(What:=searchstring, _
                                         After:=ws_sq.Range("C3"), _
                                         SearchOrder:=xlByColumns, _
               开发者_运维百科                          MatchCase:=False, _
                                         LookAt:=xlPart, _
                                         SearchDirection:=xlNext)

                On Error Resume Next
              ' keep track of where we are. If we are in the loop below and hit            
              ' LastAddress this means we have looped back to the begining.
                LastAddress = c.Address     

                'loop until we find the part
                Do Until c Is Nothing

                    found = 1
                    Dim splitter() As String

                    splitter = Split(c.Value, ",")

                    For k = 0 To UBound(splitter)
                        If splitter(k) = searchstring Then
                            firstaddress = c.Address
                            itemRow = Mid(firstaddress, 4, Len(firstaddress) - 3)
                            feederno = ws_sq.Range("F" & itemRow)
                            counter = counter - 1
                            found = 0
                            Exit For
                        End If
                    Next

                    Set c = ws_sq.Cells.FindNext(After:=c)

                   'we loop until we find our part the file, and if found
                    'we break out then.
                    If found = 0 Then
                        Exit Do
                    End If

                    If LastAddress = c.Address Then
                        Exit Do
                    End If
                Loop    ' end do until
            End With    ' end with search_range
        Next            ' end for
End Sub

Thank you.


It looks like it's not grabbing R3 right away because you start at C3, eventually it should hit it after you search through everything else. Remember the parameter "After:=" says that after that cell it will start searching, so you might want to start the search at C2, so you would need to put

After:=ws_sq.Range("C1")

Also, if you want an exact search you could put

LookAt:=xlWhole

With xlPart you'll pick up everything else. This piece of code will make you start from the last position:

Set c = ws_sq.Cells.FindNext(After:=c)

but the main search you did won't do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜