0

I need a function which will delete duplicate elements in an array in VBA, I have searched for this but none of the given functions worked for me, they always give some kind of error. So lets say I have an array of strings:

Dim strArray() As String
strArray = Split("word1, word2, word3, word1, word2, word4", ",")

I need a function which will return another "filtered array" which will contain no duplicates so it will contain "word1, word2, word3, word4" Can anyone help me? Thanks!

3
  • stackoverflow.com/a/15877730/1519058 ?! Commented Nov 12, 2016 at 10:38
  • that code gives me "Wrong number of arguments or invalid property assignment" Commented Nov 12, 2016 at 10:53
  • @Markazol, did you try my code? Commented Nov 12, 2016 at 10:59

1 Answer 1

2
Function FilterWords(words() As String) As String()
    Dim word As Variant
    Dim newWords As String

    For Each word In words
        If InStr(newWords, word) = 0 Then newWords = newWords & word & ","
    Next word
    FilterWords = Split(Left(newWords, Len(newWords) - 1), ",")
End Function

to be used like

Sub main()    
    Dim strArray() As String, strFilteredArray() As String

    strArray = Split("word1,word2,word3,word1,word2,word4", ",")        
    strFilteredArray = FilterWords(strArray) 
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.