VBA, Is there a way to check if a value is equal to any value inside array?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm using an array as a list of values that are not present in a table. However, sometimes an equal value is inputed twice.



I have used a simple condition to avoid the current value being equal to the prior, but I can't seem to find a solution for duplicate values overall.



        If k > 0 Then
If arrA(k) = arrA(k - 1) Then
arrA(k) = ""
k = k - 1
End If
End If









share|improve this question




















  • 1





    You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

    – GSerg
    Jan 4 at 18:27













  • Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

    – JNevill
    Jan 4 at 18:30













  • Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

    – JNevill
    Jan 4 at 18:35


















1















I'm using an array as a list of values that are not present in a table. However, sometimes an equal value is inputed twice.



I have used a simple condition to avoid the current value being equal to the prior, but I can't seem to find a solution for duplicate values overall.



        If k > 0 Then
If arrA(k) = arrA(k - 1) Then
arrA(k) = ""
k = k - 1
End If
End If









share|improve this question




















  • 1





    You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

    – GSerg
    Jan 4 at 18:27













  • Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

    – JNevill
    Jan 4 at 18:30













  • Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

    – JNevill
    Jan 4 at 18:35














1












1








1








I'm using an array as a list of values that are not present in a table. However, sometimes an equal value is inputed twice.



I have used a simple condition to avoid the current value being equal to the prior, but I can't seem to find a solution for duplicate values overall.



        If k > 0 Then
If arrA(k) = arrA(k - 1) Then
arrA(k) = ""
k = k - 1
End If
End If









share|improve this question
















I'm using an array as a list of values that are not present in a table. However, sometimes an equal value is inputed twice.



I have used a simple condition to avoid the current value being equal to the prior, but I can't seem to find a solution for duplicate values overall.



        If k > 0 Then
If arrA(k) = arrA(k - 1) Then
arrA(k) = ""
k = k - 1
End If
End If






arrays excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 6 at 20:10









T.M.

2,4153933




2,4153933










asked Jan 4 at 18:24









Raul MRaul M

83




83








  • 1





    You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

    – GSerg
    Jan 4 at 18:27













  • Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

    – JNevill
    Jan 4 at 18:30













  • Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

    – JNevill
    Jan 4 at 18:35














  • 1





    You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

    – GSerg
    Jan 4 at 18:27













  • Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

    – JNevill
    Jan 4 at 18:30













  • Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

    – JNevill
    Jan 4 at 18:35








1




1





You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

– GSerg
Jan 4 at 18:27







You can take IsInArray from stackoverflow.com/a/3244429/11683, or just switch to a Collection which makes duplicate tracking much easier.

– GSerg
Jan 4 at 18:27















Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

– JNevill
Jan 4 at 18:30







Perhaps a dictionary or collection would be a better choice than an array. It's tough to say since it's not clear what you are using the array for (like if you are making use of the order of the elements downstream).

– JNevill
Jan 4 at 18:30















Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

– JNevill
Jan 4 at 18:35





Also, if the array is reasonably small you could do an ugly version of IsInArray() in a one-liner If InStr("|" & Join(arrA, "|") & "|", "|" & arrA(K) & "|") Then

– JNevill
Jan 4 at 18:35












1 Answer
1






active

oldest

votes


















0














Repeated filtering method



Just for the sake of the art, I demonstrate an approach via ► repeated filtering (i.e. without using a dictionary or collection - you'll find numerous dict/coll examples at SO :-):



Example code



This example assumes string values to be checked for duplicates (using case sensitivity in repeated filtering in a tricky way - cf. argument vbBinaryCompare in Filter function) and matching the current index position (idx) of each search term.



Option Explicit                     ' declaration head of code module
Sub DupEx()
' Purpose: delete subsequent string duplicates in array
' Method: repeated filter function using match to get index position
' Site: https://stackoverflow.com/questions/54044235/vba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array
' Author: T.M. (https://stackoverflow.com/users/6460297/t-m)
Dim arrA(), i&, idx&, flt, searched
' example string values (change to wanted values)
arrA = Array("zero", "one", "two", "two", "three", "two", "four", "zero", "four", "three", "five", "five")
flt = arrA
Debug.Print "Original array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
For i = LBound(arrA) To UBound(arrA)
searched = arrA(i) ' define search term
If UBound(Filter(flt, searched, True, vbBinaryCompare)) > 0 Then
'[1] change first occurrence of search term to temporary dummy (to avoid later deletion)
On Error Resume Next
idx = Application.Match(searched, flt, False) - 1 ' deduct 1 as match result is one based
flt(idx) = ChrW(&H2999) ' change to temporary dummy value
'[2] execute filter (3rd argument=False DELETES each subsequent search value in flt array)
' [Caveat: this example deletes partial string findings as well !]
flt = Filter(flt, searched, False, vbBinaryCompare)
'[3] restore first occurrence back to old value
flt(idx) = searched
End If
Next i
Debug.Print "Filtered array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
'arrA = flt ' overwrite original array
End Sub


Debug.Print result as shown in the immediate window of the Visual Basic Editor (VBE)



  Original array counts 12 elements: "zero, one, two, two, three, two, four, zero, four, three, five, five"
Filtered array counts 6 elements: "zero, one, two, three, four, five"





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044235%2fvba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Repeated filtering method



    Just for the sake of the art, I demonstrate an approach via ► repeated filtering (i.e. without using a dictionary or collection - you'll find numerous dict/coll examples at SO :-):



    Example code



    This example assumes string values to be checked for duplicates (using case sensitivity in repeated filtering in a tricky way - cf. argument vbBinaryCompare in Filter function) and matching the current index position (idx) of each search term.



    Option Explicit                     ' declaration head of code module
    Sub DupEx()
    ' Purpose: delete subsequent string duplicates in array
    ' Method: repeated filter function using match to get index position
    ' Site: https://stackoverflow.com/questions/54044235/vba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array
    ' Author: T.M. (https://stackoverflow.com/users/6460297/t-m)
    Dim arrA(), i&, idx&, flt, searched
    ' example string values (change to wanted values)
    arrA = Array("zero", "one", "two", "two", "three", "two", "four", "zero", "four", "three", "five", "five")
    flt = arrA
    Debug.Print "Original array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
    For i = LBound(arrA) To UBound(arrA)
    searched = arrA(i) ' define search term
    If UBound(Filter(flt, searched, True, vbBinaryCompare)) > 0 Then
    '[1] change first occurrence of search term to temporary dummy (to avoid later deletion)
    On Error Resume Next
    idx = Application.Match(searched, flt, False) - 1 ' deduct 1 as match result is one based
    flt(idx) = ChrW(&H2999) ' change to temporary dummy value
    '[2] execute filter (3rd argument=False DELETES each subsequent search value in flt array)
    ' [Caveat: this example deletes partial string findings as well !]
    flt = Filter(flt, searched, False, vbBinaryCompare)
    '[3] restore first occurrence back to old value
    flt(idx) = searched
    End If
    Next i
    Debug.Print "Filtered array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
    'arrA = flt ' overwrite original array
    End Sub


    Debug.Print result as shown in the immediate window of the Visual Basic Editor (VBE)



      Original array counts 12 elements: "zero, one, two, two, three, two, four, zero, four, three, five, five"
    Filtered array counts 6 elements: "zero, one, two, three, four, five"





    share|improve this answer






























      0














      Repeated filtering method



      Just for the sake of the art, I demonstrate an approach via ► repeated filtering (i.e. without using a dictionary or collection - you'll find numerous dict/coll examples at SO :-):



      Example code



      This example assumes string values to be checked for duplicates (using case sensitivity in repeated filtering in a tricky way - cf. argument vbBinaryCompare in Filter function) and matching the current index position (idx) of each search term.



      Option Explicit                     ' declaration head of code module
      Sub DupEx()
      ' Purpose: delete subsequent string duplicates in array
      ' Method: repeated filter function using match to get index position
      ' Site: https://stackoverflow.com/questions/54044235/vba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array
      ' Author: T.M. (https://stackoverflow.com/users/6460297/t-m)
      Dim arrA(), i&, idx&, flt, searched
      ' example string values (change to wanted values)
      arrA = Array("zero", "one", "two", "two", "three", "two", "four", "zero", "four", "three", "five", "five")
      flt = arrA
      Debug.Print "Original array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
      For i = LBound(arrA) To UBound(arrA)
      searched = arrA(i) ' define search term
      If UBound(Filter(flt, searched, True, vbBinaryCompare)) > 0 Then
      '[1] change first occurrence of search term to temporary dummy (to avoid later deletion)
      On Error Resume Next
      idx = Application.Match(searched, flt, False) - 1 ' deduct 1 as match result is one based
      flt(idx) = ChrW(&H2999) ' change to temporary dummy value
      '[2] execute filter (3rd argument=False DELETES each subsequent search value in flt array)
      ' [Caveat: this example deletes partial string findings as well !]
      flt = Filter(flt, searched, False, vbBinaryCompare)
      '[3] restore first occurrence back to old value
      flt(idx) = searched
      End If
      Next i
      Debug.Print "Filtered array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
      'arrA = flt ' overwrite original array
      End Sub


      Debug.Print result as shown in the immediate window of the Visual Basic Editor (VBE)



        Original array counts 12 elements: "zero, one, two, two, three, two, four, zero, four, three, five, five"
      Filtered array counts 6 elements: "zero, one, two, three, four, five"





      share|improve this answer




























        0












        0








        0







        Repeated filtering method



        Just for the sake of the art, I demonstrate an approach via ► repeated filtering (i.e. without using a dictionary or collection - you'll find numerous dict/coll examples at SO :-):



        Example code



        This example assumes string values to be checked for duplicates (using case sensitivity in repeated filtering in a tricky way - cf. argument vbBinaryCompare in Filter function) and matching the current index position (idx) of each search term.



        Option Explicit                     ' declaration head of code module
        Sub DupEx()
        ' Purpose: delete subsequent string duplicates in array
        ' Method: repeated filter function using match to get index position
        ' Site: https://stackoverflow.com/questions/54044235/vba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array
        ' Author: T.M. (https://stackoverflow.com/users/6460297/t-m)
        Dim arrA(), i&, idx&, flt, searched
        ' example string values (change to wanted values)
        arrA = Array("zero", "one", "two", "two", "three", "two", "four", "zero", "four", "three", "five", "five")
        flt = arrA
        Debug.Print "Original array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
        For i = LBound(arrA) To UBound(arrA)
        searched = arrA(i) ' define search term
        If UBound(Filter(flt, searched, True, vbBinaryCompare)) > 0 Then
        '[1] change first occurrence of search term to temporary dummy (to avoid later deletion)
        On Error Resume Next
        idx = Application.Match(searched, flt, False) - 1 ' deduct 1 as match result is one based
        flt(idx) = ChrW(&H2999) ' change to temporary dummy value
        '[2] execute filter (3rd argument=False DELETES each subsequent search value in flt array)
        ' [Caveat: this example deletes partial string findings as well !]
        flt = Filter(flt, searched, False, vbBinaryCompare)
        '[3] restore first occurrence back to old value
        flt(idx) = searched
        End If
        Next i
        Debug.Print "Filtered array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
        'arrA = flt ' overwrite original array
        End Sub


        Debug.Print result as shown in the immediate window of the Visual Basic Editor (VBE)



          Original array counts 12 elements: "zero, one, two, two, three, two, four, zero, four, three, five, five"
        Filtered array counts 6 elements: "zero, one, two, three, four, five"





        share|improve this answer















        Repeated filtering method



        Just for the sake of the art, I demonstrate an approach via ► repeated filtering (i.e. without using a dictionary or collection - you'll find numerous dict/coll examples at SO :-):



        Example code



        This example assumes string values to be checked for duplicates (using case sensitivity in repeated filtering in a tricky way - cf. argument vbBinaryCompare in Filter function) and matching the current index position (idx) of each search term.



        Option Explicit                     ' declaration head of code module
        Sub DupEx()
        ' Purpose: delete subsequent string duplicates in array
        ' Method: repeated filter function using match to get index position
        ' Site: https://stackoverflow.com/questions/54044235/vba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array
        ' Author: T.M. (https://stackoverflow.com/users/6460297/t-m)
        Dim arrA(), i&, idx&, flt, searched
        ' example string values (change to wanted values)
        arrA = Array("zero", "one", "two", "two", "three", "two", "four", "zero", "four", "three", "five", "five")
        flt = arrA
        Debug.Print "Original array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
        For i = LBound(arrA) To UBound(arrA)
        searched = arrA(i) ' define search term
        If UBound(Filter(flt, searched, True, vbBinaryCompare)) > 0 Then
        '[1] change first occurrence of search term to temporary dummy (to avoid later deletion)
        On Error Resume Next
        idx = Application.Match(searched, flt, False) - 1 ' deduct 1 as match result is one based
        flt(idx) = ChrW(&H2999) ' change to temporary dummy value
        '[2] execute filter (3rd argument=False DELETES each subsequent search value in flt array)
        ' [Caveat: this example deletes partial string findings as well !]
        flt = Filter(flt, searched, False, vbBinaryCompare)
        '[3] restore first occurrence back to old value
        flt(idx) = searched
        End If
        Next i
        Debug.Print "Filtered array counts " & UBound(flt) + 1 & " elements: " & Chr(34) & Join(flt, ", ") & Chr(34)
        'arrA = flt ' overwrite original array
        End Sub


        Debug.Print result as shown in the immediate window of the Visual Basic Editor (VBE)



          Original array counts 12 elements: "zero, one, two, two, three, two, four, zero, four, three, five, five"
        Filtered array counts 6 elements: "zero, one, two, three, four, five"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 6 at 20:37

























        answered Jan 6 at 19:57









        T.M.T.M.

        2,4153933




        2,4153933
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044235%2fvba-is-there-a-way-to-check-if-a-value-is-equal-to-any-value-inside-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas