+ Reply to Thread
Results 1 to 5 of 5

Execute Code based on a set of items contained in an array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Execute Code based on a set of items contained in an array

    If it is possible I would like to have code that executes different macros based on the content of a set of values, or the content of an array.

    I populate an array with True and False values, based on a set of selections.
    Depending on the selected options the array will contain any of the following sets:
    Set1 = {"True", "False", "True", "False", "True", "False"}
    Set2 = {"True", "False", "True", "False", "False", "True"}
    Set3 = {"True", "False", "False", "True", "True", "False"}
    Set4 = {"True", "False", "False", "True", "False", "True"}
    Set5 = {"False", "True", "False", "False", "True", "False"}
    Set6 = {"False", "True", "False", "False", "False", "True"}

    What I would like to do then is to use the Case statement something like this:

    Select Case Set
    Case 1
    program statements
    Case 2
    program statements
    Case 3
    program statements
    Case 4
    program statements
    Case 5
    program statements
    Case 6
    program statements
    End Select

    I hope this is at all possible. Thanks.

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Execute Code based on a set of items contained in an array

    If statements are generally faster than select statements, and in this case it would seem to me doubly so.

    So why not something like the following?

    'Set1 = {"True", "False", "True", "False", "True", "False"}
    'Set2 = {"True", "False", "True", "False", "False", "True"}
    'Set3 = {"True", "False", "False", "True", "True", "False"}
    'Set4 = {"True", "False", "False", "True", "False", "True"}
    'Set5 = {"False", "True", "False", "False", "True", "False"}
    'Set6 = {"False", "True", "False", "False", "False", "True"}
    
    Sub TrueFalse()
        Dim bArray(1 To 6) As Boolean
        If bArray(1) And Not bArray(2) Then
            If bArray(3) And Not bArray(4) Then
                If bArray(5) And Not bArray(6) Then
                    ' Set 1
                ElseIf Not bArray(5) And bArray(6) Then
                    ' Set 2
                End If
            ElseIf Not bArray(3) And bArray(4) Then
                If bArray(5) And Not bArray(6) Then
                    ' Set 3
                ElseIf Not bArray(5) And bArray(6) Then
                    ' set 4
                End If
            End If
        ElseIf Not bArray(1) And bArray(2) And Not bArray(3) And Not bArray(4) Then
            If bArray(5) And Not bArray(6) Then
                ' set 5
            ElseIf Not bArray(5) And bArray(6) Then
                ' set 6
            End If
        End If
    End Sub
    I didn't test this code, so there might be some mistake in it. But the idea should be correct.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,633

    Re: Execute Code based on a set of items contained in an array

    One way
    Sub test()
        Dim mySet(1 To 6), myArray, i As Long, msg As String
        mySet(1) = [{True, False, True, False, True, False}]
        mySet(2) = [{True, False, True, False, False, True}]
        mySet(3) = [{True, False, False, True, True, False}]
        mySet(4) = [{True, False, False, True, False, True}]
        mySet(5) = [{False, True, False, False, True, False}]
        mySet(6) = [{False, True, False, False, False, True}]
        myArray = Array(True, False, False, True, True, False)
        For i = 1 To 6
            If Join$(myArray) = Join$(mySet(i)) Then
                msg = i
                Exit For
            End If
        Next
        MsgBox IIf(Len(msg), "mySet" & msg, "No match")
    End Sub

  4. #4
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Execute Code based on a set of items contained in an array

    Thanks Jindon, I will try this out as well. I don't fully understand what the code does but will try and figure it out.

  5. #5
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Execute Code based on a set of items contained in an array

    Thanks a million, it works like a charm!

+ 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