【发布时间】:2025-11-30 17:30:01
【问题描述】:
我正在尝试在sheets("formula").range("D3") 中使用vlookup 并将其自动填充到D3 与sheets("formula") 中最后一行和最后一列中的单元格之间的所有单元格中。查找值位于 sheets("combined") 中,它在数量或行数方面具有动态范围。到目前为止,这是我的代码:
Sub Vlookup ()
Dim lastcol As Integer
With Sheets("Formula")
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Dim lastrow As Long
With Sheets("Formula")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Dim comlast As Long
With Sheets("Combined")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Sheets("Formula").Range("D2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC3&"" | ""&R1C,Combined!R2C3:R" & comlast & "C4, 2, FALSE),"" "")"
Sheets("Formula").Range("D2").AutoFill Destination:=Sheets("Formula").Range("D2:D" & lastrow), Type:=xlFillDefault
Sheets("Formula").Range("D2:D" & lastrow).AutoFill Destination:=Sheets("Formula").Range(Cells(2, "D"), Cells(lastrow, lastcol)), Type:=xlFillDefault
End Sub
我收到运行时错误“1004”:应用程序定义或对象定义错误。
任何帮助将不胜感激
【问题讨论】:
-
在第三个
With块中,您使用的是lastrow而不是comlast。 -
谢谢!没能捡到。我已经更正了,但仍然出现同样的错误
-
现在您需要在分配公式之前调试和检查这三个值。
-
你还需要qualify嵌套
Cells(lastrow, lastcol)与工作表对象。 -
你在哪一行得到错误?