如何在不同的工作表上运行语法

我遇到了一些麻烦,创build一个将在两张不同的工作表上运行我的VLOOKUP语法的macros。 我有两张纸(ABC和XYZ)。 我想我的macros运行两个不同的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 

添加一个'。' 在所有的细胞调用之前。 你已经使用with块,但没有引用任何地方的工作表。 例如

 LastRow = .Cells.Find(What:="*", After:=.Cells(1, 1), Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 

没有引用工作表,你隐式引用ActiveSheet.Cells