我需要一个用于不同工作表中多个匹配项的宏,它将返回唯一 ID 的所有值。
- 表 1 包含唯一的 ID 和值,可能存在重复的值。
- 在另一张纸上,也可能有重复的值,并且将为每个重复值赋值对应的值
- 作为输出,我需要一个唯一 ID 的所有相应值。
Sheet1 的内容
id1 isin1
id2 isin1
id3 isin2
id4 isin3
Sheet2的内容:
isin1 value1 age1
isin1 value2 age2
isin2 value3 age3
isin3 value4 age4
isin3 value5 age5
isin1 value6 age6
isin3 value7 age7
<U>Output</U>
id1 isin1 value1 age1
id1 isin1 value2 age2
id1 isin1 value6 age6
id2 isin1 value1 age1
id2 isin1 value2 age2
id2 isin1 value6 age6
id3 isin2 value3 age3
id4 isin3 value4 age4
id4 isin3 value5 age5
id4 isin3 value7 age7
我尝试对此进行编程,但出现错误。
我试图循环两个数组,即(range1 & range2)并尝试将所有值存储在另一个数组(range3)中。
运行程序时出现运行时 424 对象错误。
如何在另一个数组中以期望的方式获取值?
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim varSheetC As Variant
Dim strRangeToCheck1 As String
Dim strRangeToCheck2 As String
Dim strRangeToCheck3 As String
Dim iRow1 As Long
Dim iCol1 As Long
Dim iRow2 As Long
Dim iCol2 As Long
Dim iRow3 As Long
Dim iCol3 As Long
strRangeToCheck1 = "A1:B5"
strRangeToCheck2 = "D1:E6"
strRangeToCheck3 = "f1:h22"
Debug.Print Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck1)
varSheetB = Worksheets("Sheet1").Range(strRangeToCheck2)
varSheetC = Worksheets("Sheet1").Range(strRangeToCheck3)
Debug.Print Now
For iRow1 = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol1 = LBound(varSheetA, 2) To UBound(varSheetA, 2)
For iRow2 = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol2 = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA(iRow1, 2) = varSheetB(iRow2, 1) Then
varSheetC(iRow1, 1).Value = varSheetA(iRow1, 1).Value " Here i am getting 424 runtime error"
Else
End If
Next iCol2
Next iRow2
Next iCol1
Next iRow1
End Sub