【问题标题】:VBA - Remove both items from array when not uniqueVBA - 当不唯一时从数组中删除这两个项目
【发布时间】:2012-07-31 10:34:23
【问题描述】:

我一直在努力解决的快速问题。我有 2 个包含字符串的不同长度的数组。 我想输出一个新数组,如果检测到重复,则删除两个元素。目前,它只删除重复项,但留下不正确的原始文件。

例如

input = array ("cat","dog","mouse","cat")  
expected output =  array ("dog","mouse")  
actual output = array ("cat","dog","mouse")  

代码如下:

Sub removeDuplicates(CombinedArray)
Dim myCol As Collection
Dim idx As Long
Set myCol = New Collection

On Error Resume Next

For idx = LBound(CombinedArray) To UBound(CombinedArray)
    myCol.Add 0, CStr(CombinedArray(idx))
    If Err Then
        CombinedArray(idx) = Empty
        dups = dups + 1
        Err.Clear
    ElseIf dups Then
        CombinedArray(idx - dups) = CombinedArray(idx)
        CombinedArray(idx) = Empty
    End If
Next

For idx = LBound(CombinedArray) To UBound(CombinedArray)
    Debug.Print CombinedArray(idx)
Next
removeBlanks (CombinedArray)
End Sub

提前感谢所有帮助和支持。

【问题讨论】:

  • 使用冒泡排序对数组进行排序,然后删除重复项:)
  • 嗨 Siddharth,冒泡排序算法不是针对数值的吗?
  • 不,没有必要:) 您可以对任何类型的数组进行冒泡排序。
  • 事实上还有一种不用排序的方法,那就是使用唯一的集合。
  • 再次感谢 Siddhart 的反馈,唯一的收藏是否只会删除重复项?如果检测到重复,我需要它来删除原始和重复。

标签: windows arrays excel vba unique


【解决方案1】:

使用Scripting.Dictionary 怎么样?像这样:

Function RemoveDuplicates(ia() As Variant)

Dim c As Object
Set c = CreateObject("Scripting.Dictionary")
Dim v As Variant
For Each v In ia
    If c.Exists(v) Then
        c(v) = c(v) + 1
    Else
        c.Add v, 1
    End If
Next

Dim out() As Variant
Dim nOut As Integer
nOut = 0

For Each v In ia
    If c(v) = 1 Then
        ReDim Preserve out(nOut) 'you will have to increment nOut first, if you have 1-based arrays
        out(nOut) = v
        nOut = nOut + 1
    End If
Next

RemoveDuplicates = out

End Function

【讨论】:

    【解决方案2】:

    这是一个简单的例子。如果您遇到任何错误,请告诉我。

    Sub Sample()
        Dim inputAr(5) As String, outputAr() As String, temp As String
        Dim n As Long, i As Long
    
        inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
        inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
    
        BubbleSort inputAr
    
        For i = 1 To UBound(inputAr)
            If inputAr(i) = inputAr(i - 1) Or inputAr(i) = temp Then
                inputAr(i - 1) = "": temp = inputAr(i): inputAr(i) = ""
            End If
        Next i
    
        n = 0
        For i = 1 To UBound(inputAr)
            If inputAr(i) <> "" Then
                n = n + 1
                ReDim Preserve outputAr(n)
                outputAr(n) = inputAr(i)
            End If
        Next i
    
        For i = 1 To UBound(outputAr)
            Debug.Print outputAr(i)
        Next i
    End Sub
    
    Sub BubbleSort(arr)
        Dim value As Variant
        Dim i As Long, a As Long, b As Long, c As Long
    
        a = LBound(arr): b = UBound(arr)
    
        Do
            c = b - 1
            b = 0
            For i = a To c
                value = arr(i)
                If (value > arr(i + 1)) Xor False Then
                    arr(i) = arr(i + 1)
                    arr(i + 1) = value
                    b = i
                End If
            Next
        Loop While b
    End Sub
    

    编辑

    另一种不排序的方式

    Sub Sample()
        Dim inputAr(5) As String, outputAr() As String
        Dim n As Long, i As Long, j As Long
        Dim RemOrg As Boolean
    
        inputAr(0) = "cat": inputAr(1) = "Hen": inputAr(2) = "mouse"
        inputAr(3) = "cat": inputAr(4) = "dog": inputAr(5) = "Hen"
    
        For i = 0 To UBound(inputAr)
            For j = 1 To UBound(inputAr)
                If inputAr(i) = inputAr(j) Then
                    If i <> j Then
                        inputAr(j) = "": RemOrg = True
                    End If
                End If
            Next
            If RemOrg = True Then
                inputAr(i) = ""
                RemOrg = False
            End If
        Next i
    
        n = 0
        For i = 0 To UBound(inputAr)
            If inputAr(i) <> "" Then
                n = n + 1
                ReDim Preserve outputAr(n)
                outputAr(n) = inputAr(i)
            End If
        Next i
    
        For i = 1 To UBound(outputAr)
            Debug.Print outputAr(i)
        Next i
    End Sub
    

    【讨论】:

    • 嗨,Siddarth,在示例上做得很好!在测试数据上运行良好,但是当我将它部署到我的实际数据上时,有 20 条记录说“重复”或“唯一”,另外 25 条说同样,我看到重复元素?请问您能提供支持吗?
    猜你喜欢
    • 2022-11-17
    • 2019-05-17
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多