所以我有一个文件的宏,其中有三列。宏检测我的三个列圆柱文件的另一个工作簿中的特定列和行。有关练习册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
有任何帮助吗?感谢。
答案 0 :(得分:1)
我应该先考虑这个公式:
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