+ Reply to Thread
Results 1 to 3 of 3

Returning from a Function

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    68

    Returning from a Function

    Hello,

    I have a function that has some Case statements that contain a For loop. The general purpose of the code is to match a parameter to the appropriate case then iterate through the For loop until a value is matched from a list of roles. When that value is matched, I would like the function to terminate immediately and return the matched value.

    This is some pseudocode for the function. My problem is that even though I have the return statement (function name is set to a value) the for loop is processed to the end rather than terminating immediately. In my mind, this wastes time and is inefficient. Should I use an Exit Function statement? I most commonly see that associated with returning errors.

    Thanks in advance for the help!

    
    Function Test (param1 as String) As Currency
    
    Case param1 = 1
    
    for i = 0 to i =35
    
    if( list(i) = param1) then
    Test = i 'I would like the function to terminate immediately here rather than contine to process
    end if
    
    
    End Function

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

    Re: Returning from a Function

    Eh, not seeing any Select Case in that code.

    Anyway, try using Exit Function immediately after you've set the value.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    68

    Re: Returning from a Function

    Thanks, i will give that a go.

+ 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. VBA Function Returning #VALUE! Error
    By GazB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-23-2013, 04:16 PM
  2. [SOLVED] if function returning 0 when it should not
    By scientia in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-12-2012, 12:34 AM
  3. Returning value from Function
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2012, 10:49 PM
  4. Nested IF Function Returning All Zero
    By lady_Jane in forum Excel General
    Replies: 2
    Last Post: 06-29-2011, 04:22 PM
  5. returning more than one value from a VBA function
    By MJH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2005, 09:06 PM

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