+ Reply to Thread
Results 1 to 7 of 7

Thread: muliple searches and loops

  1. #1
    Registered User
    Join Date
    12-20-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    muliple searches and loops

    there is a piece of code called GeneFinder() and it works perfectly for finding one instance of the "gene names in sheet1" from the list in sheet3. However, if there is more than one instance of the gene names in sheet1 it will only find the first one and then move on to the next term from sheet3. I was wondering if there was anyway to do a "findAll" sort of function for the items in sheet3?

    Sub GeneFinder()
    Dim srchLen, gName, nxtRw As Integer
    Dim g As Range
    'Clear Sheet 2 and Copy Column Headings
     Sheets(2).Cells.ClearContents
     Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
    'Determine length of Search Column from Sheet3
       srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
    'Loop through list in Sheet3, Column A. As each value is
    'found in Sheet1, Column D, copy it top the next row in Sheet2
      With Sheets(1).Columns("D")
        For gName = 2 To srchLen
          Set g = .Find(Sheets(3).Range("A" & gName), lookat:=xlPart)
            If Not g Is Nothing Then
              nxtRw = Sheets(2).Range("D" & Rows.Count).End(xlUp).Row + 1
              g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
            End If
        Next
      End With
    End Sub
    i'm not great with loops, and need to identify where to apply the loop to find more than the 1 instance of the name from sheet 3.

    help would be gratefully recieved!

  2. #2
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: muliple searches and loops

    Give this a try. Since i am unable to see your workbook this may or may not do what you want.

    Option Explicit
    
    Sub GeneFinder()
    Dim srchLen, gName, nxtRw As Integer
    Dim g As Range
    Dim startaddress As Variant
    
    Application.ScreeUpdating = False
    'Clear Sheet 2 and Copy Column Headings
     Sheets(2).Cells.ClearContents
     Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
    'Determine length of Search Column from Sheet3
       srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
    'Loop through list in Sheet3, Column A. As each value is
    'found in Sheet1, Column D, copy it top the next row in Sheet2
      With Sheets(1).Columns("D")
        For gName = 2 To srchLen
          Set g = .Find(Sheets(3).Range("A" & gName), lookat:=xlPart)
            If Not g Is Nothing Then
              nxtRw = Sheets(2).Range("D" & Rows.Count).End(xlUp).Row + 1
              g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
            End If
          startaddress = g.Address
            Do
              If Not g Is Nothing Then
                nxtRw = Sheets(2).Range("D" & Rows.Count).End(xlUp).Row + 1
                g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
              End If
              Set g = .FindNext(g)
            Loop While Not g Is Nothing And g.Address <> startaddress
            Application.ScreenUpdating = True
        Next
      End With
    End Sub

  3. #3
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    657

    Re: muliple searches and loops

    stnkynts,


    Not tested, but I used the filtered approach.
    In the code I et the filtered range from Column A to J. The filter will be set to the value in column D and it should copy the visible cells to the next empty row in sheet2.


    
    
    
    Sub GeneFinder()
    Dim srchLen, gName, lrow As Long
    Dim Myval As Integer
    Dim VisRng As Range
    'Clear Sheet 2 and Copy Column Headings
    Sheets(2).Cells.ClearContents
        'activate sheet3 and set filter
    Sheets("Sheet3").Activate
        '' set filter range ''
        Range("A1:J1").Select
        Selection.AutoFilter
        '' loop thru column D in sheet 1
        For gName = 2 To Sheets("Sheet1").Range("D65536").End(xlUp).Row
            With Selection
                .AutoFilter Field:=1, Criteria1:=Sheets("Sheet1").Cells(gName, 4).Text '' this set the filtered data for the value
                With ActiveSheet.AutoFilter.Range
                    On Error Resume Next
                    Set VisRng = .Resize(.Rows.Count - 1, 1).Offset(1, 0) _
                        .Cells.SpecialCells(xlCellTypeVisible)
                        If Err <> 1004 Then
                            '' make sure you have more than 1 row  ''
                            lrow = ActiveSheet.Range("A65536").End(xlUp).Row
                            Myval = 0
                            Myval = .Range("A1:A" & lrow).SpecialCells(xlCellTypeVisible).Count
                            If Myval >= "2" Then
                            ''' this should copy visible cells from coulmn A to I
                                Range(Cells(VisRng.Offset(1, 0).Row, 1), Cells(Range("A65536").End(xlUp).Row, 9)) _
                                        .Copy Destination:=Sheets("Sheet2").Range("A" & Sheets("Sheet2").Range("A65536").End(xlUp).Row + 1)
                            End If
                        End If
                End With
            End With
        Next
    End Sub
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  4. #4
    Registered User
    Join Date
    12-20-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: muliple searches and loops

    Hi guys. cheers for the efforts!

    The approach that stnkynts used almost meets the requirement. This works brilliantly when only doing a search on 1 value listed in sheet 3. But when there is a long list of values in sheet 3, the code errors at the following point:
    startaddress = g.Address
    Also, i had to amend the following line Application.ScreeUpdating = False to Application.ScreenUpdating = False

    I have attached my workbook with the amended code for you to have a look at.

    Thanks
    Last edited by nervous_pilchard; 07-22-2011 at 10:26 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: muliple searches and loops

    Here you go: I made some adjustments to shorten/clean up the code

    Option Explicit
    
    Sub GeneFinder()
    Dim srchLen As Long, gName As Long, lastrow As Long
    Dim g As Range
    Dim startaddress As Variant
    
    Application.ScreenUpdating = False
    'Clear Sheet 2 and Copy Column Headings
     Sheets(2).Cells.ClearContents
     Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
    'Determine length of Search Column from Sheet3
       srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
       lastrow = Sheets(1).Range("D" & Rows.Count).End(xlUp).Row
    'Loop through list in Sheet3, Column A. As each value is
    'found in Sheet1, Column D, copy it top the next row in Sheet2
        For gName = 2 To srchLen
          Set g = Sheets(1).Range("D2:D" & lastrow).Find(Sheets(3).Range("A" & gName), lookat:=xlPart)
            If Not g Is Nothing Then
              startaddress = g.Address
            Do
              g.EntireRow.Copy Destination:=Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
              Set g = Sheets(1).Range("D2:D" & lastrow).FindNext(g)
            Loop While Not g Is Nothing And g.Address <> startaddress
            End If
        Next
    Application.ScreenUpdating = True
    
    End Sub

  6. #6
    Registered User
    Join Date
    12-20-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: muliple searches and loops

    stnkynts - thank you very much for taking the time to refine the code.

    i really appreciate it!

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: muliple searches and loops

    Or
    Sub GeneFinder()
      Sheets(2).usedrange.offset(1).ClearContents
      sn=sheets(3).columns(1).specialcells(2)
    
      with sheets(1).usedrange
        for j=1 to ubound(sn)
          .autofilter 4,sn(j,1)
          .offset(1).copy sheets(2).cells(rows.count,1).end(xlup).offset(1)
          .autofilter
        next
      end with
    end sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0