+ Reply to Thread
Results 1 to 7 of 7

Do While only evaluating one of two clauses?

  1. #1
    Registered User
    Join Date
    01-29-2012
    Location
    rsareceffects
    MS-Off Ver
    Excel 2010
    Posts
    7

    Question Do While only evaluating one of two clauses?

    Hi,

    I have a Do While loop with two clauses, but the 2nd one appears to not be evaluated properly, or i'm missing something trivial.

    with the example below, if you put HI somewhere in a column, and then select a cell below it in the same column you should get "Found" in the immediate window. If you select a column without HI it will continue looking up the column until it error's when it trys to look at Row Zero.

    Im a little confused as my clause to detect that error and stop it seems to be ignored.

    Please Login or Register  to view this content.
    Can anyone shed light on why this is happening?

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Do While only evaluating one of two clauses?

    Hi

    As you are decreasing the variable row, with a starting value of 0, it will go negative. The test source.row - row is effectively a double negative, making it a positive result so it will always be > 0.

    Also, shouldn't your evaluation for found / not found be done on result not source?

    HTH

    rylo
    Last edited by rylo; 01-29-2012 at 09:18 PM.

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Do While only evaluating one of two clauses?

    A few things = the value for row is negative so source.row - row (postive minus a negative number) will always be greater than zero so
    Please Login or Register  to view this content.
    Otherwise you will try to get the value of a cell with a row number = 0
    Secondly Source will never be Nothing so change
    Please Login or Register  to view this content.
    Your whole code should be
    Please Login or Register  to view this content.
    It is an around about way of doing what you are after (finding the word "Hi") in a column. Another way would be
    Please Login or Register  to view this content.
    Hope this helps.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”

  4. #4
    Registered User
    Join Date
    01-29-2012
    Location
    rsareceffects
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Do While only evaluating one of two clauses?

    Yes Source should have been Result in the example, its correct in my working code, just wrote the example wrong

    Negative numbers are no fun, I've been looking at that clause for a while today trying to figure out why, was a pretty simple mistake as I thought.

    I'll look into changing the loops to Find()'s though as its probably a whole lot faster.

    Do you know if its possible to explicitly look upward from the current cell? because there could be multiple instances of the things im looking for, and it does need to be the one closest to the selection.

    Thanks for the help

  5. #5
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Do While only evaluating one of two clauses?

    I don't know if you can search upwards apart from iterating each cell in a range as you did in your previous code however you could find all the values with the folowing
    Please Login or Register  to view this content.
    Otherwise use you code as I posted in my previous post with the corrections so the first found cell will be the cell closest to the active cell.
    Hope this helps.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Do While only evaluating one of two clauses?

    Hi

    Searching backwards can be done using the searchdirection parameter.
    Please Login or Register  to view this content.
    rylo

  7. #7
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Do While only evaluating one of two clauses?

    Thanks rylo - I was sure there would be with the searchDirection parameter. Learn something new everyday

  8. #8
    Registered User
    Join Date
    01-29-2012
    Location
    rsareceffects
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Do While only evaluating one of two clauses?

    That looks like it should do the trick!

    Thanks a lot

+ 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