+ Reply to Thread
Results 1 to 12 of 12

Erratic behaviour of .Find

  1. #1
    Registered User
    Join Date
    12-02-2012
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    40

    Erratic behaviour of .Find

    When testing a subroutine, using exactly the same code, sometimes the .Find function works and sometimes it doesn't. Once it's stopped working it refuses to work again, but may have been fine numerous times beforehand. Advice as to what could be wrong would be much appreciated—I don't want to have to resort to the "For Each c in Range" method of finding what I'm looking for (it's bulletproof but much slower, of course).

    In the code excerpt below the first Finds (and two similar others) work perfectly every time—they are used to find and set the ranges (rWk and rMon) to be searched. With the troublesome Find (searching for data in rWk), commenting out these initial Finds makes no difference, neither does adding variants (LookIn, LookAt, etc) as the experts say we should. The troublesome Find is simply looking for integers <=31 in a range of no more than 12 cells, but needs to do so hundreds of times during the routine so Find would be much more efficient.

    Any help or advice much appreciated.

    Please Login or Register  to view this content.
    Last edited by goneps; 12-03-2012 at 12:17 AM.

  2. #2
    Forum Expert Moo the Dog's Avatar
    Join Date
    09-28-2012
    Location
    Wisconsin
    MS-Off Ver
    Office 365 (PC & Mac)
    Posts
    1,845

    Re: Erratic behaviour of .Find

    VBA beginner here, but have you tried declaring 's' as a Range instead of string?
    - Moo

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Try using some of the other arguments Find takes, not just what you are looking for.
    If posting code please use code tags, see here.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Erratic behaviour of .Find

    as Norie said you need to use all the arguments. find remembers settings between uses and not just code uses-if you run a find in the excel ui those settings will affect your code too unless you use all the arguments
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Erratic behaviour of .Find


  6. #6
    Registered User
    Join Date
    12-02-2012
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Erratic behaviour of .Find

    Many thanks to all for replies. I'd already tried those solutions, without success. Without having had the opportunity to do much further testing I believe Jindon has come up with the answer on another forum. For the record, the first line of code should be changed to:
    Please Login or Register  to view this content.
    In other words, don't put Activate on the same line (which causes it to reference an action, not an object).

    Nonetheless all suggestions greatly appreciated.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Erratic behaviour of .Find

    if you do not change the .Find line to include additional arguments you will continue to run into problems-you can make book on that :-)

    additionally you don't have to activate
    Please Login or Register  to view this content.
    notice the periods before the Range calls.

    and finally you should always test the result of a find to be sure it isn't Nothing before you try and use that range
    Last edited by JosephP; 12-03-2012 at 04:42 PM.

  8. #8
    Registered User
    Join Date
    12-02-2012
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Erratic behaviour of .Find

    Thanks, Joseph. I'm well aware of the need to include additional arguments and normally make a point of doing so, but while testing to overcome this problem they seemed either to make no difference or to make matters worse.

    Speaking of which, neither the VBA Help nor the massive VBA book I have tells me what to use for the After:= argument. Presumably its an integer or long, so is "0" used to start the search at the first cell in the range?

    I'm also aware that Activate is not essential, but having forgotten most of what I knew about VBA through years of disuse I need to watch what's happening. I write what I think will work and then deal with the problems. Yes, I know, not exactly competent, but it's like having to learn to walk all over again.

    Quote Originally Posted by JosephP View Post
    notice the periods before the Range calls.
    Thanks for reminding me about that—easily overlooked.

    Quote Originally Posted by JosephP View Post
    and finally you should always test the result of a find to be sure it isn't Nothing before you try and use that range
    Yep, it's in the code but I left it out of the example for simplicity's sake.

    Thanks again for your help, I'm always grateful for advice.

    Richard
    Last edited by goneps; 12-03-2012 at 06:24 PM.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Erratic behaviour of .Find

    hi, Richard,
    the After argument is an optional Range object-one of the few arguments that you can usually leave out

  10. #10
    Registered User
    Join Date
    12-02-2012
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Erratic behaviour of .Find

    With the modification suggested by Jindon all seemed to be working well, but then the erratic behaviour started again. Having wasted so much time and effort attempting to get to the bottom of the problem I've given up and re-written the code to place all the "Bills" list addresses in arrays using For Each Cell in Range, which is totally bulletproof.

    This way it's only necessary to search for payments once, after which they're accessed from the relevant array. Much more reliable than the cantankerous, desperately unreliable Find method (and probably a better means of execution, too).

    Nevertheless, grateful thanks to all who've tried to help.

    (Message also appended to x-post at http://www.ozgrid.com/forum/showthre...233#post639233)

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Erratic behaviour of .Find

    For what it's worth you do not need to Activate the worksheet if you qualify everything properly.

    For example here none of the Ranges are tied to 'Bills'.
    Please Login or Register  to view this content.
    To tie the ranges to the workbook only needs a little dot.
    Please Login or Register  to view this content.
    If you had qualified those ranges then the code might have worked.

  12. #12
    Registered User
    Join Date
    12-02-2012
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Erratic behaviour of .Find

    JosephP reminded me about the missing full stops so I corrected that (and tried about a million other things), but it still made no difference. 'Activate' is only there for the purposes of debugging; once I no longer need to see what's happening they'll be removed.

    In any event, I believe the revised arrangement using arrays to store the information instead of looking for it each time is a better method. Were I more expert I might have done it that way in the first place.
    Last edited by goneps; 12-05-2012 at 05:45 PM.

+ 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