Excel VBA - 评估和索引函数返回#VALUE!错误
Excel VBA - Evaluate and index function returning #VALUE! error
所以我有一个文件宏,其中有三列。宏为我的三列文件检测另一个工作簿中的特定列和行。请参见下图的工作簿 1。
然后它应该查看工作簿 2 的行和列(见下文)并找到与工作簿 1 匹配的列 A 和 B 的位置,并将列 C 的相应值粘贴到工作簿 2 中(值1).但是我不断得到#VALUE!错误,我不知道为什么会这样。
仅供参考 - 列继续为 51,但无法放入图像
下面是这个宏的代码:
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
所以我有一个文件宏,其中有三列。宏为我的三列文件检测另一个工作簿中的特定列和行。请参见下图的工作簿 1。
然后它应该查看工作簿 2 的行和列(见下文)并找到与工作簿 1 匹配的列 A 和 B 的位置,并将列 C 的相应值粘贴到工作簿 2 中(值1).但是我不断得到#VALUE!错误,我不知道为什么会这样。
仅供参考 - 列继续为 51,但无法放入图像
下面是这个宏的代码:
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