+ Reply to Thread
Results 1 to 5 of 5

Exit Function doesn't break out of For loop?

  1. #1
    Registered User
    Join Date
    06-08-2007
    Posts
    15

    Exit Function doesn't break out of For loop?

    Am writing a simple function to test of the existence of a worksheet in the current workbook

    The code below evaluates to false each time even though WsName exists in the workbook wb.

    Please Login or Register  to view this content.
    A little investigation reveals this is because the Exit Function statement isn't causing an exit of the Function.

    My rewrite is:-

    Please Login or Register  to view this content.
    The rewrite works, just wondered why the Exit Function doesn't work in scenario 1?

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon odeno

    just wondered why the Exit Function doesn't work
    As you have discovered, the Exit Function isn't designed to break out of a For ... Next loop.

    To do this use Exit For, which will jump to the next statement outside of the loop. Other uses for Exit are :
    Exit Do
    Exit Property
    Exit Sub


    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    In your first macro the for seems to be incorrect. You wrote:
    Please Login or Register  to view this content.
    but I think you wanted to write:
    Please Login or Register  to view this content.
    The second macro don't need the exit function because the 'do while.... loop' statement ends when found is true but if you use 'exit function' it doesn't create problems.

    In your macro, if you write sheet name to look for in lower case it doesn' find it. It's better if you use lcase or ucase conversione, for example:
    Please Login or Register  to view this content.
    Regards,
    Antonio

  4. #4
    Registered User
    Join Date
    06-08-2007
    Posts
    15
    Thanks folks.

    I spotted the for typo after posting. I like the second form of the function.........

    thanks again.

  5. #5
    Registered User
    Join Date
    06-08-2007
    Posts
    15
    Interesting point - setting
    Please Login or Register  to view this content.
    Doesn't cause the function to return?

+ 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