+ Reply to Thread
Results 1 to 4 of 4

Clarification on Find and FindNext methods

  1. #1
    Forum Contributor
    Join Date
    08-20-2019
    Location
    Mumbai
    MS-Off Ver
    Office 365
    Posts
    101

    Clarification on Find and FindNext methods

    Hi,

    Pls help me understand this behavior of Find method in my code.

    This code searches a contacts table for one or more occurrence of a Customer name and picks corresponding contact names and populates a listbox with found contacts.

    When I step into this code to test for the customer (Mergermarket), i noticed that with the first Find statement the cell found is B5 and not B2 (Pls see attached image). Why would it not find B2 first? B2 gets found later with FindNext statement.


    Please Login or Register  to view this content.
    Attached Images Attached Images

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Clarification on Find and FindNext methods

    The Find statement has another optional attribute, not shown in the code above, which allows for a starting point 'After" a specified cell reference within the search range. If you want the search to begin in the first cell of the range, then set the 'After' reference to the last cell of the range. For Example if 'tcContacts_Cust' refers to $B$2:$B$100 then the statement would be:
    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    08-20-2019
    Location
    Mumbai
    MS-Off Ver
    Office 365
    Posts
    101

    Re: Clarification on Find and FindNext methods

    Thanks for your reply. I was under the impression that the Find statement would start search from the first cell in the range. Instead it starts the search after the first cell.
    https://docs.microsoft.com/en-us/off...6)%26rd%3Dtrue

    The last para in the description of After parameter is this:
    If you do not specify this argument, the search starts after the cell in the upper-left corner of the range.


    That's why cell B2 was not found first. So I changed the range 'tcContacts_Cust' to include the column header, which solves the problem. Now It starts the search from the second cell which is the actual data range.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Clarification on Find and FindNext methods

    Yep, works either way, extend the range upward or use the last cell in the range for the 'After' parameter if it is not finding the first occurrence first.
    Thanks for the feedback, and don't forget to mark the thread as solved.
    Regards, JLG

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Trouble with Find & FindNext methods
    By gdary in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2015, 02:29 PM
  2. find value in column with FindNext
    By nono in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2010, 04:35 PM
  3. Cells.find and .findnext
    By Buzzaro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2007, 08:29 AM
  4. Question on FindNext vs Find
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-18-2006, 10:00 PM
  5. Using Find & FindNext in a form
    By BernzG in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2005, 07:28 PM
  6. [SOLVED] Nesting Find and FindNext
    By SA3214 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-10-2005, 04:05 PM
  7. Using 'Find' and 'FindNext' in vba
    By SA3214 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-25-2005, 09:06 AM

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