+ Reply to Thread
Results 1 to 5 of 5

Exit Function doesn't break out of For loop?

Hybrid View

odeno Exit Function doesn't break... 06-19-2007, 10:28 AM
dominicb Good afternoon odeno As... 06-19-2007, 10:49 AM
antoka05 In your first macro the for... 06-19-2007, 11:02 AM
odeno Thanks folks. I spotted... 06-19-2007, 11:34 AM
odeno Interesting point - setting ... 06-19-2007, 11:51 AM
  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.

    Private Function WorkSheetExists(ByRef WsName As String, wb As Workbook) As Boolean
        
        Dim i As Long  
        
        For i = 1 to i= wb.Sheets.Count
            If (StrComp(WsName, wb.Sheets(i).Name) = 0) Then          
                WorkSheetExists = True
                Exit Function
            End If
            
           Next i
    
        WorkSheetExists = False
        
    End Function
    A little investigation reveals this is because the Exit Function statement isn't causing an exit of the Function.

    My rewrite is:-

    Private Function WorkSheetExists(ByRef WsName As String, wb As Workbook) As Boolean
        
        Dim i As Long
        Dim Found As Boolean
        
        Found = False
        i = 1
        
        Do While Not Found And i <= wb.Sheets.Count
            If (StrComp(WsName , wb.Sheets(i).Name) = 0) Then           
                Found = True
                WorkSheetExists = Found
    '            Exit Function  'not needed as it doesn't exit function...but why?
            End If       
            
            i = i + 1
        Loop
        
        WorkSheetExists = Found
        
    End Function
    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:
    For i = 1 to i= wb.Sheets.Count
    but I think you wanted to write:
    For i = 1 to wb.Sheets.Count
    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:
    Private Function WorkSheetExists(ByRef WsName As String, wb As Workbook) As Boolean
        Dim i As Long
        
        WorkSheetExists = False
        For Each ws In wb.Sheets
            If LCase(WsName) = LCase(ws.Name) Then
                WorkSheetExists = True
                Exit Function
            End If
        Next
    End Function
    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
    WorkSheetExists = False
    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