我有2个动态数组,第一个数组包含15,000个值,并且位于Sheet1上。第二个数组具有519个值,位于Sheet2上。
第一个数组中的所有值都应位于第二个数组中,因为第一个数组包含重复的值。
我想做的是将第一个数组与第二个数组进行比较,并返回匹配数的%。 (应该是100%)。
所示代码仅包含动态数组和用于检查数组的L边界和U边界的公式的开头。但是,我对公式应该说的东西感到困惑。
Dim ws As Worksheet
Dim wb As Workbook
Dim ISRC() As Variant
Dim ISRC2() As Variant
Set wb = Workbooks("Recordssales2019-04-05")
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = wb.Worksheets("Recordssales2019-04-")
Set ws2 = wb.Worksheets("Metadata")
Lastrow = ws1.Range("E100000").End(xlUp).Row
ReDim ISRC(1 To Lastrow + 1)
MsgBox Lastrow
Lastrow = ws2.Range("AJ100000").End(xlUp).Row
ReDim ISRC2(1 To Lastrow + 1)
MsgBox Lastrow
For i = LBound(ISRC) To UBound(ISRC)
If ISRC(i) = ISRC2(i) Then
理想情况下,将第一个数组与第二个数组进行比较,并返回匹配的相同数组。从那里,我希望能够根据它们是否匹配进行下一步。如果它们不匹配,则不应将它们包括在下一步中。
答案 0 :(得分:0)
尝试一下。一口气从数组中填充数组。遍历ISRC,使用MATCH检查每个元素是否在另一个数组中,并保持计数。
Sub x()
Dim ws As Worksheet
Dim wb As Workbook, lastrow As Long, i As Long, v As Variant, count As Long
Dim ISRC As Variant
Dim ISRC2 As Variant
Set wb = Workbooks("Recordssales2019-04-05")
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = wb.Worksheets("Recordssales2019-04-")
Set ws2 = wb.Worksheets("Metadata")
lastrow = ws1.Range("E100000").End(xlUp).Row
ISRC = Application.Transpose(ws1.Range("E1:E" & lastrow).Value)
'MsgBox lastrow
lastrow = ws2.Range("AJ100000").End(xlUp).Row
ISRC2 = Application.Transpose(ws2.Range("AJ1:AJ" & lastrow).Value)
'MsgBox lastrow
For i = LBound(ISRC) To UBound(ISRC)
v = Application.Match(ISRC(i), ISRC2, 0)
If IsNumeric(v) Then count = count + 1
Next i
MsgBox count & " elements of ISRC are in ISRC2 (" & Format(count / UBound(ISRC), "0.0%") & ")."
End Sub