我正在开发一个新项目,并且是VBA的新手。我必须将公式填充到C2到B列的最后一行。我的代码如下
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2").Select
Range("C2:C" & LastRow).Formula = "=VLOOKUP(B2,'[Tracking Sheet Opens.xlsb]Data'!$B$1:$J$65530,9,0)"
这提示我为错误1004 - 方法范围或对象全局失败
请建议我前进。
答案 0 :(得分:0)
我不确定PO中VLookup
的搜索范围是否在"跟踪"工作簿中的工作表,或者数据"数据"另一个工作簿中的工作表名为"跟踪表打开" ,所以我的答案涵盖了两种情况。
Option Explicit
Sub CopyVLookupFormulaDown()
Dim LastRow As Long
With Sheets("Sheet2")
' find last row with data in Column A in "Sheet2"
'LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
' from post maybe need to look for last row in Column B
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
' Option 1: check value of cell B2 in "sheet2" with the range
' in sheet "Data" in another workbook named "Tracking Sheet Opens"
.Range("C2").Formula = "=VLOOKUP(B2,'[Tracking Sheet Opens.xlsb]Data'!$B$1:$J$65530,9,0)"
' Option 2: check value of cell B2 in "sheet2" with the range
' in sheet "Tracking" in the same workbook
.Range("C2").Formula = "=VLOOKUP(B2,'Tracking'!$B$1:$J$65530,9,0)"
' same line for both options, copy formula down
.Range("C2:C" & LastRow).FillDown
End With
End Sub