自动填充公式不是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”。
- Range.Calculate不更新单元格中的值
- Excel – VBA代码中断,如果我重命名我的Excel文件 – 有没有解决?
- VBscript读取excel – 1.如何获取特定列的行数,2 – 获取特定行的列数
- 如何使用vba删除excel中的空单元格
- 将xls读取为数据库时,确定Excel文件的第一个工作表名称
- VBA Excel:ColorIndex兼容性的RGB范围
- 自动筛选电子表格齿轮
- 无法将“Microsoft.Office.Interop.Excel.ApplicationClass”types的COM对象转换为接口types“Microsoft.Office.Interop.Excel._Application”
- 迫使excel显示一个负数($ 0,000)