Results 1 to 4 of 4

Thread: [RESOLVED] Quickie simple question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Resolved [RESOLVED] Quickie simple question

    I'm sure by now everybody is tired of me and my questions, but this one I'm sure is simple and quick, but I've been unable to find exactly the answer in searches of this forum and others.

    How can I search a particular column of cells for a certain value and get the row number of where that value is?

    i.e. search column B for a cell with "apple" in it, and get the row of where it is.

    I'm thinking worksheetfunction.find, maybe? Although I don't know how to use that, and I'm not sure that's what I need.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Quickie simple question

    Yes you can do this

    1, Declare a Range on the column - Select a Range of Cells - e.g. A1 TO A100

    go to - Insert / Name / Define - and give it a name e.g. - "SearchValues" .

    Then -

    Loop through the Range e.g -

    Sub Test ()

    Dim vSearch as String

    vSearch = Search("apple")

    msgbox "Row count is " & vSearch

    end sub

    Private function Search(strSearchVal as String) as Long

    Dim RowCount as String
    Dim n as String

    n = 1

    For Each cell In Range("SearchValues")
    If cell.Value = strSearchVal then
    RowCount = n
    Exit For
    End If
    n = n + 1
    Next

    Search = RowCount

    End Function
    Last edited by NeedSomeAnswers; Sep 30th, 2005 at 04:31 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    southwest VA
    Posts
    136

    Re: Quickie simple question

    I actually ended up finding a function that will do what I want for this,
    iRow = Range("C:C").Find(gstrNam, , , xlWhole).Row

    That works ok to find ONE instance of something, where the something is unique in that column. But what if I want to find the LAST occurence of something in a column where there are multiple occurences?

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Quickie simple question

    You cound write a new function, like Find, but working backwards through the cells in your range.


    VB Code:
    1. Function FindLast(fValue As String, fRange As Range) As Long
    2. Dim x As Long
    3.    
    4.     If fRange.Columns.Count <> 1 Then
    5.         MsgBox "fRange should have only one column."
    6.         FindLast = -1
    7.         Exit Function
    8.     End If
    9.    
    10.     For x = fRange.Rows.Count To 1 Step -1
    11.         If fRange.Cells(x, 1).Value = fValue Then
    12.             FindLast = fRange.Cells(x, 1).Row
    13.             Exit Function
    14.         End If
    15.     Next x
    16.    
    17.     FindLast = 0
    18. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width