Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using FIND to return row number (VBA Excel 2000)

    I was reading a web page which was extolling the virtues of using inbuilt Excel functions over loops. Of particular interest was the relative efficiency of the .find function over looping through an unsorted range. I'm wanting to write a procedure that will search for a particular string in column C of a worksheet, using the .find function, and return the row number of the "found" cell.

    I'm having a few difficulties here. A recorded macro will include a Columns("C:C").Select, then a Selection.Find statement. If I try to "abbreviate" this to a Columns("C:C").Find (or any variation I can think of) Excel informs me that my specified range is not a valid one. I'm also wondering how to look for only exact matches using .find. For instance, searching for "AS345" would not recognize "AS345-2" as a match. The .Find function returns a range, so I'm guessing that the .Row property would be the appropriate way to retrieve the row number.

    Any advice on this appreciated

    Alan

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Using FIND to return row number (VBA Excel 2000)

    Something like this:

    Dim rngSearch As Range, rngFound As Range
    Set rngSearch = Range("C:C")
    Set rngFound = rngSearch.Find(What:="AS345", LookIn:=xlValues, LookAt:=xlPart)
    If rngFound Is Nothing Then
    MsgBox "Not found"
    Else
    MsgBox rngFound.Row
    End If

    The LookAt argument specifies that it's OK if the What argument matches part of the cell value.

  4. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using FIND to return row number (VBA Excel 2000)

    Thanks Hans. I thought that for once I might have a chance at beating you to it, but I see not <img src=/S/grin.gif border=0 alt=grin width=15 height=15>. I managed to nut out something similar using some examples I found:

    Dim loc
    With Range("C:C")
    Set loc = .Find(What:="as345", LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False)
    If Not loc Is Nothing Then MsgBox loc.Row
    End With

    The "xlWhole" was the "exact match" part that was eluding me.

    I'm still not sure why my original incarnation didn't work. I'm sure it's (still) my ignorance of the use of range objects. The help file says:

    expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte)
    expression Required. An expression that returns a Range object.

    I can't see why Columns("C:C") does not qualify as a correct expression. ???

    Alan

  5. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Using FIND to return row number (VBA Excel 2000)

    If I use Set rng = Columns("C:C") or With Columns("C:C") it works for me too.

  6. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using FIND to return row number (VBA Excel 2000)

    Hehe... now it works for me too! Must have been something silly I did writing the code. Still, I'm glad to know it's my stupidity rather than lack of understanding that's causing me problems <img src=/S/grin.gif border=0 alt=grin width=15 height=15>.

    Alan

Posting Permissions

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