我需要一个能在VBA中删除数组中重复元素的函数,我已经搜索了这个但是没有一个给定的函数对我有用,它们总是会出现某种错误。所以我要说我有一个字符串数组:
Dim strArray() As String
strArray = Split("word1, word2, word3, word1, word2, word4", ",")
我需要一个函数,它将返回另一个“过滤后的数组”,它不包含重复项,因此它将包含“word1,word2,word3,word4”任何人都可以帮助我吗? 谢谢!
答案 0 :(得分: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
像
一样使用Sub main()
Dim strArray() As String, strFilteredArray() As String
strArray = Split("word1,word2,word3,word1,word2,word4", ",")
strFilteredArray = FilterWords(strArray)
End Sub