简体   繁体   English

填写公式直到列中的最后一行

[英]Fill down formula till last row in column

I have a formula (I2-J2) in cell K2 and I want Excel to Copy that down till the last row of my data set.我在单元格 K2 中有一个公式(I2-J2),我希望 Excel 将其复制到我的数据集的最后一行。 So take the formula in K2 and copy it to K3, K4, K5 and so on.所以取K2中的公式,复制到K3、K4、K5等。 Which row is the last row changes dynamically each month based on the data set so I cant use fixed values.哪一行是最后一行,每个月根据数据集动态变化,所以我不能使用固定值。

I already tried the other solution in a similar thread: Fill formula down till last row in column but that unfortunately did not work.我已经在类似的线程中尝试了其他解决方案: 将公式向下填充到列中的最后一行,但不幸的是这不起作用。

Sub Autofill()
Dim LastRow As Long
LastRow = Range("K" & Rows.Count).End(xlUp).Row
Range("K2:K" & LastRow).Formula = I2 - J2
End Sub

The macro already stops in cell K2, which is not the end of my data set and even the formula gives out the wrong result 0 instead of value in I2 minus value in J2宏已经在单元格 K2 中停止,这不是我的数据集的结尾,甚至公式给出了错误的结果 0 而不是 I2 中的值减去 J2 中的值

What did I do wrong here?我在这里做错了什么?

Many thanks!非常感谢!

This should be what you need:这应该是您需要的:

Sub AutoPhill()
    Dim N As Long
    N = Cells(Rows.Count, "J").End(xlUp).Row
    Range("K2:K" & N).Formula = "=I2-J2"
End Sub

Note the the formula addresses adjust for each row.请注意,公式地址会针对每一行进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM