【问题标题】:Create third array from difference of two arrays从两个数组的差异创建第三个数组
【发布时间】:2016-02-09 13:51:06
【问题描述】:

我需要从两个数组的差异中创建第三个数组,我根本无法理解这个逻辑

正确的第三个数组 v3 将是(来自下面的代码)v3 = (Carol, Ted, Thor, Freya)

谢谢

Sub MatchArrays()
Dim v1, v2, v3
Dim i As Long, j As Long

v1 = Array("Bob", "Carol", "Ted", "Alice", "Thor", "Freya")
v2 = Array("Bob", "Carol")

ReDim v3(LBound(v1) To Abs(UBound(v1) - UBound(v2)))

For i = LBound(v1) To UBound(v1)
   For j = LBound(v2) To UBound(v2)
     If InStr(1, v1(i), v2(j)) Then
       v3(i) = v1(i)
       Exit For
     End If
   Next j
  MsgBox v3(i)
Next i
End Sub

【问题讨论】:

  • 一个数组总是成为另一个数组的子集吗?
  • 那为什么会是所示示例的结果?
  • @Tim,是的,一个永远是另一个的子集。 @shg, v3 是我想要得到的结果
  • 我认为问题是如果 Carol 同时在 v1 和 v2 中,为什么 Carol 在 v3 中?似乎 v3 应该是“Ted”、“Alice”、“Thor”、“Freya”,因为它们只在 v1 中。
  • @DougGlancy,你的权利应该是v3 = (Ted, Alice, Thor, Freya)

标签: excel vba


【解决方案1】:

这个使用集合,并且添加重复键会引发错误。 v1 或 v2 是否是子集并不重要:

Sub test()
Dim v1 As Variant, v2 As Variant, v3 As Variant
Dim coll As Collection
Dim i As Long

'Assumes 0-based Variants
v1 = Array("Bob", "Carol", "Ted", "Alice", "Thor", "Freya")
v2 = Array("Bob", "Carol")
ReDim v3(LBound(v1) To Abs(UBound(v2) - UBound(v1)) - 1)

Set coll = New Collection
For i = LBound(v1) To UBound(v1)
    coll.Add v1(i), v1(i)
Next i
For i = LBound(v2) To UBound(v2)
    On Error Resume Next
    coll.Add v2(i), v2(i)
    If Err.Number <> 0 Then
        coll.Remove v2(i)
    End If
    On Error GoTo 0
Next i
For i = LBound(v3) To UBound(v3)
    v3(i) = coll(i + 1) 'Collections are 1-based
    Debug.Print v3(i)
Next i
End Sub

【讨论】:

  • 我之所以选择这个是因为它可以做到,所以我不必知道哪个是子集,谢谢
【解决方案2】:

试试这个:

Sub Test()

Dim v1, v2, v3
Dim i As Long, j As Long
Dim str As String

v1 = Array("Bob", "Carol", "Ted", "Alice", "Thor", "Freya")
v2 = Array("Bob", "Carol")

ReDim v3(LBound(v1) To Abs(UBound(v1) - UBound(v2)))

For i = LBound(v1) To UBound(v1) 'Assuming, that v2 is always the subset of v1

str = v1(i)

If Not IsInArray(str, v2) Then

v3(j) = v1(i)
MsgBox v3(j)
j = j + 1

End If

Next

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

【讨论】:

  • 前几天我遇到了类似的问题,偶然发现了这个article
  • 值得注意的是过滤器匹配子字符串:Join(Filter(Array("Timothy","Tim"),"Tim"),",") ->> "Timothy,Tim"
  • 这是真的!感谢您指出了这一点!人们可能不得不使用Lensomewhere 来检查stringToBeFound 和找到的字符串是否具有相同的长度。
【解决方案3】:

我相信这就是你想要的:

Sub MatchArrays()
Dim v1, v2, v3
Dim i As Long, j As Long
Dim here As Boolean
Dim f As Long
v1 = Array("Bob", "Carol", "Ted", "Alice", "Thor", "Freya")
v2 = Array("Bob", "Carol")
f = LBound(v1)
ReDim v3(LBound(v1) To Abs(UBound(v1) - UBound(v2)))

For i = LBound(v1) To UBound(v1)
    here = False
    For j = LBound(v2) To UBound(v2)
        If InStr(1, v1(i), v2(j)) Then
            here = True
            Exit For
        End If
    Next j
    If Not here Then
        v3(f) = v1(i)
        MsgBox v3(f)
        f = f + 1
    End If
Next i
End Sub

【讨论】:

  • 谢谢,我可以关注,我知道会发生什么
猜你喜欢
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 2019-06-27
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
相关资源
最近更新 更多