简体   繁体   English

VBA 自动填充范围调整

[英]VBA Autofill Range Adjustment

I have recorded a macro in Excel, but when I run it with updated data, it only applies to the number of rows I had when I first had the macro recorded.我在 Excel 中记录了一个宏,但是当我使用更新的数据运行它时,它只适用于我第一次记录宏时的行数。 I dove into the macro code and I think these are the problems:我深入研究了宏代码,我认为这些是问题所在:

Selection.AutoFill Destination:=Range("L2:R2"), Type:=xlFillDefault
    Range("L2:R2").Select
    Selection.AutoFill Destination:=Range("L2:R242")
    Range("L2:R242").Select

How can I adjust the macro to have the range set up as the size of the worksheet?如何调整宏以将范围设置为工作表的大小?

Save the last row of your data into a variable and then modifiy the line to always fill the formula to that value like this:将数据的最后一行保存到变量中,然后修改该行以始终将公式填充为该值,如下所示:

Dim Last_Row as Long
Last_Row = Application.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("L2:R2").AutoFill Destination:=Range("L2:R" & Last_Row), Type:=xlFillDefault

(Rows.Count, 1) this 1 represents the Column A. I supposed your data is here (Rows.Count, 1) 这个 1 代表 A 列。我想你的数据在这里

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

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