VBA(Microsoft Access)功能删除阵列中的重复项 [英] VBA (Microsoft Access) Function to delete duplicates in Array

查看:108
本文介绍了VBA(Microsoft Access)功能删除阵列中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,该函数将删除VBA中数组中的重复元素,我已经进行了搜索,但是给定的函数都不对我有用,它们总是会产生某种错误。因此,可以说我有一个字符串数组:

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", ",")

我需要一个函数将返回另一个过滤数组,该数组将不包含重复项,因此将包含 word1,word2,word3,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!

推荐答案

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

这篇关于VBA(Microsoft Access)功能删除阵列中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆