简体   繁体   English

如何在VBA中自动填充功能?

[英]How to autofill a function in VBA?

I am trying to autofill and have found code for the problem however, Upon the autofill completing its action all of the cells simply produce the same result as from the original cell. 我正在尝试自动填充并找到问题的代码但是,在自动填充完成其操作后,所有单元格只会产生与原始单元格相同的结果。

Sub Button2_Click()


Range("P2").Value = Application.Sum(Range(Cells(2, 18), Cells(2, 40)))

Range("P2").Select
Selection.AutoFill Destination:=Range("P2:P5"), Type:=xlFillDefault

End Sub

I'm not sure exactly what you're looking for, but if you're looking to use the SUM function, perhaps this one-liner: 我不确定你到底在寻找什么,但如果你想使用SUM函数,也许这个单行:

Range("P2:P5").Formula = "=SUM(Q2:AN2)"

You can write the formula to the range in one go, no need to AutoFill . 您可以一次性将公式写入范围,无需AutoFill

If you just want the values, then add a second line to convert formulas to values: 如果您只想要值,则添加第二行以将公式转换为值:

Range("P2:P5").Value = Range("P2:P5").Value

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

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