Excel VBA – 评估和索引函数返回#VALUE! 错误
所以我有一个macros的文件,其中有三列。 macros检测我的三个柱面文件的另一个工作簿中的特定列和行。 请参阅下面的图像的工作簿1。
然后,应查看工作簿2的行和列(请参见下文),并find与工作簿1匹配的列A和B的位置,并将C列的相应值粘贴到工作簿2(值为1)中。 不过,我一直在获得#VALUE! 错误,我不知道为什么发生。
仅供参考 – 列数继续为51,但不适合图像
下面是这个macros的代码:
Sub Location() Dim i As Long, k As Long, ws1 As Worksheet, ws2 As Worksheet Dim lastrow As Long Set ws1 = Workbooks("Book3.xlsm").Worksheets("Sheet1") Set ws2 = Workbooks("Book4.xlsm").Worksheets("Sheet1") lastrow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row For k = 2 To ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column ws2.Cells(i, k).Value = ws2.Evaluate("IFERROR(INDEX(" & ws1.Range("B1:B" & lastrow).Address(0, 0, xlA1, 1) & ",AGGREGATE(15,6,ROW(" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & ")" & _ "/((" & ws1.Range("A1:A" & lastrow).Address(0, 0, xlA1, 1) & " = " & ws2.Cells(i, 1).Address(0, 0) & ")*(" & ws1.Range("C1:C" & lastrow).Address(0, 0, xlA1, 1) & "=" & _ ws2.Cells(1, k).Address(0, 0) & ")),1)),"""")") Next k Next i End Sub
任何帮助? 谢谢。
我应该首先想到这个公式:
Sub Location() Dim i As Long, k As Long, ws1 As Worksheet, ws2 As Worksheet Dim LastRow As Long Set ws1 = Workbooks("Book3.xlsm").Worksheets("Sheet1") Set ws2 = Workbooks("Book4.xlsm").Worksheets("Sheet1") LastRow = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row For i = 2 To ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row For k = 2 To ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column ws2.Cells(i, k).Value = Application.WorksheetFunction.SumIfs(ws1.Range("C1:C" & LastRow), ws1.Range("A1:A" & LastRow), ws2.Cells(i, 1), ws1.Range("B1:B" & LastRow), ws2.Cells(1, k)) If ws2.Cells(i, k).Value = 0 Then ws2.Cells(i, k).Value = "" Next k Next i End Sub