-1

我需要一个用于不同工作表中多个匹配项的宏,它将返回唯一 ID 的所有值。

  1. 表 1 包含唯一的 ID 和值,可能存在重复的值。
  2. 在另一张纸上,也可能有重复的值,并且将为每个重复值赋值对应的值
  3. 作为输出,我需要一个唯一 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 
4

1 回答 1

0

所以如果我理解正确的话,第一张纸上只有 1 个 id#?如果是这种情况,请遍历 sheet1 行并读取每一对 (id#, isin#)。

然后,对于每个 isin#,您在 sheet2 中搜索 isin# 并获得另一对 (value#, age#)。你现在有一组 4 输出。当然,您必须阅读 sheet2 的所有 isin#s,因此将其放入循环中。作为一个简单的例子 - (编辑 - 比我的原始版本更多的代码,因为我有 Excel 可以调试):

Option Explicit

Sub Tester()

Dim counter1 As Long
Dim counter2 As Long
Dim counter3 As Long
Dim counter4 As Long

Dim firstSheet1Row As Long
Dim lastSheet1Row As Long
Dim firstSheet2Row As Long
Dim lastSheet2Row As Long

Dim firstColNumSht1 As Long
Dim secondColNumSht1 As Long
Dim firstColNumSht2 As Long
Dim secondColNumSht2 As Long
Dim thirdColNumSht2 As Long

Dim writeRow As Long

Dim dataVal(1 To 4) As Long

firstSheet1Row = 1
lastSheet1Row = 10
firstSheet2Row = 1
lastSheet2Row = 20

firstColNumSht1 = 1
secondColNumSht1 = 2
firstColNumSht2 = 1
secondColNumSht2 = 2
thirdColNumSht2 = 3

writeRow = 1

For counter1 = firstSheet1Row To lastSheet1Row

  dataVal(1) = Worksheets(1).Cells(counter1, firstColNumSht1)
  dataVal(2) = Worksheets(1).Cells(counter1, secondColNumSht1)

    For counter2 = firstSheet2Row To lastSheet2Row

      If Worksheets(2).Cells(counter2, firstColNumSht2) = dataVal(2) Then

        dataVal(3) = Worksheets(2).Cells(counter2, secondColNumSht2)
        dataVal(4) = Worksheets(2).Cells(counter2, thirdColNumSht2)

        For counter3 = 1 To 4
          Worksheets(3).Cells(writeRow, counter3) = dataVal(counter3)
        Next counter3

        writeRow = writeRow + 1

      End If

    Next counter2

  Next counter1


End Sub

我希望变量命名约定清楚,您没有准确指定工作表、行、列等的位置。此外,如果您有兴趣,请查看 Range.FindNext 方法。帮助文件包含一个可以摆脱第二个 for 循环的循环。如果你没有很多数据,那没关系(但这是更好的做法),但如果你有很长的值列表,本机 excel 代码(如 Range.FindNext)将运行一个数量级或者比手动编写检查每个单元格的 for 循环更快。

如果我误解了你的问题,请告诉我,我会尽量得到正确的答案(不过我确实有工作!)。再说一次,我不会这样编码。有更优雅、更强大和更有效的方法来完成这项工作,但我想先从一个可行的解决方案开始。

于 2012-12-14T06:40:46.020 回答