自动填充公式不是dynamic的

我试图自动填充一些公式,但是复制时不是更新单元格引用,而是保持相同的引用。 这是为了复制VLookup和If(和)语句而发生的。 代码的相关部分如下,评论部分下面的部分“现在让我们得到2014年的数据和下面。

Sub sidebyside() ' ' test Macro ' ' Dim lastRow2014 As Long ActiveSheet.Range("$A$10:$V$4388").AutoFilter Field:=11, Criteria1:="1" lastRow2014 = Range("A" & Rows.Count).End(xlUp).Row ' Get side-by-side data for comparison Range("E10").Value = "Full Name" Range("Y10").Value = "Full name" Range("Z10").Value = "2014 Data" Range("AA10").Value = "2016 Data" Range("Y11").Select Range("Y11").Formula = "=E11" 'Copy this down to the lastrow of the lefthand side (lastRow2014) Range("Y11").AutoFill Destination:=Range("Y11", "Y" & lastRow2014) 'Now let's get the 2014 data 'Range("I11: I" & lastRow2014).Copy Destination:=Range("Z11") Range("Z11").FormulaR1C1 = "=VLOOKUP(Y11,E: I, 5, FALSE)" Range("Z11").AutoFill Destination:=Range("Z11", "Z" & lastRow2014) 'Let's get the corresponding 2016 data. 'VLOOKUP! Range("AA11").FormulaR1C1 = "=VLOOKUP(Y11,P:T,5,FALSE)" Range("AA11").Select 'Selection.AutoFill Destination:=Range("AA3:AA" & Range( Selection.AutoFill Destination:=Range("AA11:AA" & Range("T" & Rows.Count).End(xlUp).Row) 'Now, let's calculate the percentage. The trick will be the reference. Range("AA4").FormulaR1C1 = "='Summary 2'!M3" 'This is the index from the summary tab Range("AA10").Value = "Within?" Range("AA11").Formula = "=IF(AND(Y11 <=(Z11 + ($AA$4 * Z11)), Y11 >= (Z11 - (Z11 * $AA$4))), ""WITHIN"", ""NOT"")" Range("AA11").Select Selection.FillDown 

填充第一个公式并填充它,而不是一次填充所有公式:

例如:

 Range("Y11:Y" & lastRow2014).Formula = "=E11" 

代替:

 Range("Y11").Select Range("Y11").Formula = "=E11" 'Copy this down to the lastrow of the lefthand side (lastRow2014) Range("Y11").AutoFill Destination:=Range("Y11", "Y" & lastRow2014) 

坚持这一点,你可以修改你的Z方程:

 Range("Z11:Z" & lastRow2014).Formula = "=VLOOKUP(Y11,E:I, 5, FALSE)" 

您遇到的问题是您的VLOOKUP中的额外空间。 “E:I”应该是“E:I”。