[英]Excel VBA - count the duplicates
我有兩個數組,其中包含字符串值。 讓我們考慮以下示例。
我需要找到兩個數組中重復項的數量。 在上述情況下,我需要總重復值為1(因為Array1和Array2中都包含人工智能)。 有沒有辦法在VBA中做到這一點?
此函數假定arr2是一維數組:
Function ArrDupCount(ByVal arr1 As Variant, ByVal arr2 As Variant) As Long
Dim varElement As Variant
Dim lMatch As Long
On Error Resume Next
For Each varElement In arr1
lMatch = 0
lMatch = WorksheetFunction.Match(varElement, arr2, 0)
If lMatch > 0 Then ArrDupCount = ArrDupCount + 1
Next varElement
On Error GoTo 0
End Function
要使用它:
Sub tgr()
Dim arr1 As Variant
Dim arr2 As Variant
arr1 = Array("Computer science", "Artificial Intelligence")
arr2 = Array("Eclipse", "MS", "RAD", "Linux", "Artificial Intelligence")
MsgBox ArrDupCount(arr1, arr2) ' => 1
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.