+ Reply to Thread
Results 1 to 7 of 7

Seaching a Single Column without Selecting

Hybrid View

  1. #1
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562

    Seaching a Single Column without Selecting

    How can I take the code listed below and do a search on a single column. In part of the sub I might want to seach Col B and in another part of the sub I might want to search Col J. What do I need to do to this so it will limit the search to a particular column.

    Set wsEnroll = Workbooks("Enrollment Verifier DV.xls").Sheets("0708")
      
    Set rData = wsData.Cells.Find(What:=sID, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    Sincerely,
    Jeff

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,589
    Change
    ...Cells.Find...
    to
    ...Columns("b").Find,,,

  3. #3
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Thank you! I'll give that a try. There are some days it is easier to ask than search and test for an hour.

    I appreciate the help.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This example should show you how to build what you want.
    The routine looks through searchColumn looking for the string SearchFor.
    They could be passed to your routine as arguments.

    The address of the first cell is noted, so that the loop can test when .Find loops around to the top of the range again.

    Notice that the Do Loop is inside With EndWith, so that searchColumn applies to .FindNext. Also note that .FindNext needs a starting point (the most recently found cell).

    By avoiding selection, this can be run from when a differnent sheet is active

    Sub test()
        Dim searchFor As String
        Dim searchColumn As Range
        
        Dim foundCell As Range
        Dim firstAddress As String
        
        Set searchColumn = ThisWorkbook.Sheets("sheet1").Range("A:A")
        searchFor = "a"
        
        With searchColumn
            Set foundCell = .Find(What:=searchFor, _
                            After:=.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
            
            If foundCell Is Nothing Then MsgBox "not found": Exit Sub
            firstAddress = foundCell.Address
            
            Do
                Rem process foundCell
                MsgBox foundCell.Address
                
                Rem find next cell
                Set foundCell = .FindNext(After:=foundCell)
            Loop Until foundCell.Address = firstAddress
        
        End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    mikerickson,
    Set searchColumn = ThisWorkbook.Sheets("sheet1").Range("A:A")
    Can the "A:A" of .Range("A:A") be set to a variable. I always seem to have trouble with that, but that is actually what I need. If it can be set to a variable, how do I do it.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    searchColumn is a variable.

    Having the Sub take arguments is one way to control it. Four lines would be removed and the declaration would be changed.
    The start of the sub would look like

    Sub FindStuff(searchFor as String, searchColumn As Range)
        'Dim searchFor As String: Rem remove
        'Dim searchColumn As Range: Rem remove
    
        Dim foundCell As Range
        Dim firstAddress As String
    
        'Set searchColumn = ThisWorkbook.Sheets("sheet1").Range("A:A"):Rem remove
        'searchFor = "a": Rem remove
    
         With searchColumn
            Set foundCell = .Find(What:=searchFor, _
                            After:=.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, _
                            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    Rem rest of code
    End Sub
    And then you could call the routine like

    Sub MainRoutine()
    
    Call FindStuff("Sam", ThisWorkbook.Sheets("Sheet1").Column("B"))
    
    Call FindStuff("Dorothy", ThisWorkbook.Sheets("Sheet2").Range("D:E"))
    
    End Sub
    Note that all occurances of "Dorothy" is being found in both columns D and E. The search can be restricted to any continious range.

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi all,

    Tushar's Findall function at the below link has a little more depth than what you are after but you may still find it useful...

    http://www.tushar-mehta.com/excel/tips/findall.html

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1