+ Reply to Thread
Results 1 to 10 of 10

User-defined function that mimics the behavior of case select

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    53

    User-defined function that mimics the behavior of case select

    I'm looking to create my own case select function that behaves basically like the =IF built-in function but can be used without nesting. It's hard to come by detailed tutorials on function building, especially for what I'm trying to do since it's more complicated than calculating the area of a rectangle, for example.

    The code below is what I have so far. I know it's not correct, but I don't know how to how to put it together. I'd like to have a larger set of optional arguments too, maybe 5 or 6.

    Public Function CaseSelect(Case1 As Variant, Do1 As Variant, Optional Case2 As Variant, Optional Do2 As Variant)
    
        Dim MyCase As Variant
        
        Select Case MyCase
            Case Case1
                Do1
            Case Case2
                Do2
            Case Else
        End Select
    
    End Function
    Last edited by Telperion; 03-20-2013 at 10:39 AM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,521

    Re: User-defined function that mimics the behavior of case select

    This looks like a good example to start from: http://msdn.microsoft.com/en-us/libr...ice.12%29.aspx Essentially all you are doing differently from this example is that you appear to want to put some of the function arguments as "case" comparisons. Here's another example of a simple UDF that uses some of the function's arguments as cases in the select case statement.
    Function testcase(value, case1, case2, case3)
    Select Case value
         Case "a"
            testcase = "a"
        Case case1
            testcase = case1
        Case case2
            testcase = case2
        Case case3
            testcase = case3
    End Select
    End Function
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: User-defined function that mimics the behavior of case select

    I think this will do what you want.
    But its kind of weird.
    Im not really sure why haha.
    Also, you can add more cases, I just did 1.
    And I just changed it to Sub instead of Function since it doesnt actually return anything right now.

    Public Sub CaseSelect(Case1 As Variant, Macro1 As String, Macro2 As String)
    Dim MyCase As Variant
    MyCase = 0   ' Just set it as 0 as an example
    Select Case MyCase
        Case Case1
            Application.Run Macro1
        Case Else
            Application.Run Macro2
    End Select
    End Sub
    
    Sub MyMainSub()
    Application.Run "CaseSelect", 0, "Macro1", "Macro2"
    End Sub
    
    Sub Macro1()
    MsgBox ("Case1")
    End Sub
    
    Sub Macro2()
    MsgBox ("Case2")
    End Sub
    Last edited by jason_lee_91; 03-08-2013 at 05:20 PM.

  4. #4
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: User-defined function that mimics the behavior of case select

    I think he means Do1 and Do2 as a series of commands.
    I could be wrong though.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,521

    Re: User-defined function that mimics the behavior of case select

    I think he means Do1 and Do2 as a series of commands.
    Except that he is passing Do1 and Do2 to the function through the argument list, so I don't think they can be a series of commands. He could certainly do more to do1 and do2 before returning the value.

  6. #6
    Forum Contributor
    Join Date
    07-16-2012
    Location
    Ontario Canada
    MS-Off Ver
    Excel 2010
    Posts
    124

    Re: User-defined function that mimics the behavior of case select

    Thats what I thought was kind of weird.
    So I just kind of took it to mean macro names.
    I think you are right though.
    The only thing I was thinking was that if Do1 and Do2 are just values, wouldn't that be the same as a regular Select/Case statement?

  7. #7
    Registered User
    Join Date
    09-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: User-defined function that mimics the behavior of case select

    Hope this clears things up, the function would be used as follows:

    =CaseSelect(A1>B2,sum(A1,"5"),A1=7,A1^2)

    It would read verbally as: If A1 is greater than B2, then sum A1 and 5, but if A1 is equal to 7, square the value in A1.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,521

    Re: User-defined function that mimics the behavior of case select

    I haven't got time right now to look at this in more detail. Looking at data types in this calling "statement," it looks like each case argument will be a boolean, and each Do argument will be a value (double or long depending on the calculation performed). The function's overall purpose is to determine which "case" argument is true, and return the corresponding "Do" value. Assuming you decide a Select Case statement is still the way to go, it seems like it should be as simple as
    Select case TRUE
    case case1
    caseselect=do1
    etc.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: User-defined function that mimics the behavior of case select

    I think what you want would look more like this:

    Function mySwitch(ParamArray avArg() As Variant) As Variant
        Dim i As Long
        
        If UBound(avArg) And 1 Then
            For i = 0 To UBound(avArg) Step 2
                If avArg(i) Then
                    mySwitch = avArg(i + 1)
                    Exit For
                End If
            Next i
        Else
            mySwitch = "Must have even number of arguments!"
        End If
    End Function
    E.g.,

    =mySwitch(A1>B2, SUM(A1, 5), A1=7, A1^2)
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    09-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    53

    Re: User-defined function that mimics the behavior of case select

    That works great, thanks! Just what I'm looking for.

+ 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