我试图匹配新文档中的值并从我的旧文档中提取相应的数据(我在代码中打开的文档)。但是,我在设置要查找的数据的列中无法循环。我想查找单元格 I2
& V2
存在于旧工作簿的 I 和 V 列中。然后我希望它继续下一行并检查单元格 I3
& V3
等等。
我的代码是:
Sub BCReport()
Dim wbO As Workbook
Dim wsO As Worksheet
Dim wbJune As Workbook, wsJune As Worksheet
Dim myRange As Range, myCPTRange As Range, myAllowedRange As Range
Dim JuneCPTRange As Range, JuneALLRange As Range, JuneMCPGRange As Range
Set wbO = ThisWorkbook
Set wsO = wbO.Sheets("Combined")
Set wbJune = Workbooks.Open("J:\Blue Cross 15_0615_P.xls")
Set wsJune = wbJune.Sheets("Combined")
Set myRange = wsO.Range("AI2:AI3000")
Set myCPTRange = wsO.Range("I2:I3000")
Set myAllowedRange = wsO.Range("V2:V3000")
Set JuneCPTRange = wsJune.Range("I2:I3000")
Set JuneALLRange = wsJune.Range("V2:V3000")
Set JuneMCPGRange = wsJune.Range("AI2:AI3000")
For i = 1 To myRange.Rows.Count
'For j = 1 To myRange.Columns.Count
myRange.FormulaArray = _
myRange.FormulaArray = _
"=INDEX('[Blue Cross 15_0615_P.xls]Combined'!$AI$2:$AI$790,MATCH(myCPTRange(i, 1).Value&myAllowedRange(i, 1).Value,'[Blue Cross 15_0615_P.xls]Combined'!$I$2:$I$790&'[Blue Cross 15_0615_P.xls]Combined'!$V$2:$V$790,0))"
'Next j
Next i
End Sub
我不断收到错误"运行时错误' 1004':应用程序定义或对象定义错误" 。
非常感谢任何想法!
答案 0 :(得分:0)
我录制了一个宏来准确地生成你想要做的事情,我发现它给了我R1C1符号来制作数组公式。
替换这部分代码:
For i = 1 To myRange.Rows.Count
'For j = 1 To myRange.Columns.Count
myRange.FormulaArray = _
myRange.FormulaArray = _
"=INDEX('[Blue Cross 15_0615_P.xls]Combined'!$AI$2:$AI$790,MATCH(myCPTRange(i, 1).Value&myAllowedRange(i, 1).Value,'[Blue Cross 15_0615_P.xls]Combined'!$I$2:$I$790&'[Blue Cross 15_0615_P.xls]Combined'!$V$2:$V$790,0))"
'Next j
Next i
有了这个
With myRange
.FormulaArray = "=INDEX('[Blue Cross 15_0615_P.xls]Combined'!C,MATCH(RC[-26]&RC[-13],'[Blue Cross 15_0615_P.xls]Combined'!C[-26]&'[Blue Cross 15_0615_P.xls]Combined'!C[-13],0))"
End With
看看它是否有效。请记住,这将循环出代码并将公式(和结果)直接放入 Combined.xls 表的列AI中的单元格中。它还会查看每个工作表中的整个列集,因此如果您在单元格790之后有数据,则需要对其进行调整。