我在创建一个将在两个不同的工作表上运行我的VLOOKUP语法的宏时遇到了一些麻烦。我有两张纸(ABC和XYZ)。我希望我的宏运行两个不同的VLOOKUP - 每张表一个。
如何调整我的代码以便它可以执行此操作?
Sub fillinABC()
Dim ABC As Worksheet
Dim XYZ As Worksheet
Set XYZ = Sheets("XYZ")
Set ABC = Sheets("ABC")
With ABC
LastRow = Cells.Find(What:="*", After:=Cells(1, 1), Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox LastRow
For i = 1 To LastRow
If IsEmpty(Cells(i, 1)) Then 'if cell in A is empty
Cells(i, 1).FormulaR1C1 = "=VLOOKUP(RC[2],'[Copy of Manual Recs - List.xlsx]XXX'!C1:C5,5,FALSE)" 'Lookup cell in B in Identifier A:C this is in C1(Column1):C3(Column3) form!!
End If
Next
End With
With XYZ
LastRow = Cells.Find(What:="*", After:=Cells(1, 1), Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox LastRow
For i = 1 To LastRow
If IsEmpty(Cells(i, 1)) Then 'if cell in A is empty
Cells(i, 1).FormulaR1C1 = "=VLOOKUP(RC[2],'[Copy of Manual Recs - List.xlsx]XXX'!C2:C5,4,FALSE)" 'Lookup cell in B in Identifier A:C this is in C1(Column1):C3(Column3) form!!
End If
Next
End With
End Sub
答案 0 :(得分:1)
添加'。'在所有Cell调用之前。您已经使用了with block,但没有在任何地方引用工作表。例如
LastRow = .Cells.Find(What:="*", After:=.Cells(1, 1), Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
在不引用工作表的情况下,您隐式引用ActiveSheet.Cells