Consulting

Results 1 to 13 of 13

Thread: Help with Excel 'find' please

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    VBAX Regular
    Joined
    Apr 2006
    Location
    Leeds
    Posts
    51
    Location
    Hi Killian,

    Once again, thanks again for the help with the code.

    Yesterday I finally had chance to have a play with it.
    I did find a couple of things that I needed to tweak.
    The first one being this line [vba]If cel1.Value <> "" Then '"0"[/vba]
    I found that if the I left the value as 0 this would cause a problem if the cell
    containing the four digit code did't have anything in it.For some reason I got a delay.
    I think this was because if no value had been found the search would then repeat over itself looking for nothing.
    This loop would go on to the end of the work sheet because of this[vba]If counter = ActiveSheet.Columns.Count Then Exit Do[/vba]

    I also added the next bit of code to insert a value if nothing was returned by the search.[vba]Else
    Worksheets(1).Cells(cel1.Row, cel1.Column + counter).Value = "0"
    End If[/vba]

    I also had to shuffle the columns around, due to the way the columns were laid out
    some of the 'filelist' was being wiped out by the search results.

    If I've read the code wrong regarding 'find' loop would you let me know please,
    being a newbie I could have got it all wrong, thanks.

    Regards,

    Nick

    P.S. Here's the final code:-

    [VBA]Sub FindCodesV4_2()

    Dim rngToSearch As Range
    Dim cel1 As Range
    Dim c As Range
    Dim counter As Integer
    Dim firstAddress As String

    Dim codeListRange As String
    codeListRange = "B" & Range("A12").Value & ":B" & Range("A14").Value
    MsgBox "Cell range = " & codeListRange

    Set rngToSearch = Worksheets(1).Range("D22001")
    For Each cel1 In Worksheets(1).Range(codeListRange)
    'counter holds the number of successful finds for each cel1
    'used as a column index offset when adding the result to Worksheets("Image Names")
    counter = 3
    If cel1.Value <> "" Then
    Set c = rngToSearch.find(What:=cel1.Value, LookIn:=xlValues, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    If Not c Is Nothing Then
    firstAddress = c.Address
    Do
    Worksheets(1).Cells(cel1.Row, cel1.Column + counter).Value = c
    counter = counter + 1

    'check for the column limit
    If counter = ActiveSheet.Columns.Count Then Exit Do
    Set c = rngToSearch.FindNext(c)
    Loop While Not c Is Nothing And c.Address <> firstAddress
    Else
    Worksheets(1).Cells(cel1.Row, cel1.Column + counter).Value = "0"
    End If
    End If
    Next cel1

    Range("A16").Select

    End Sub[/VBA]
    Last edited by Tecnik; 05-24-2006 at 08:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •