[英]How to put a range.formula into a loop in Excel VBA
first of all I would like to apologize as I am not a programmer, but I am now forced to act as in order to successfully complete a task assigned to me. 首先,我不是程序员,对此我深表歉意,但是为了成功完成分配给我的任务,我现在不得不这样做。 I am building an excel dashboard and I needed to use some VBA programming.
我正在构建一个Excel仪表板,我需要使用一些VBA编程。 All good so far whit the hints I have got from you, but currently I am stuck in a loop...:))
到目前为止一切顺利,我已经从您那里得到了提示,但目前我陷入了困境... :))
I am trying to write a specific formula, always the same in a range of cells calculated by the software. 我正在尝试编写一个特定的公式,该公式在该软件计算的单元格范围内始终相同。 Actually in the loop below everything works:
实际上,在下面的循环中,一切正常:
For i = 1 To LastCYMonth_RNG.Rows.count
LastCYMonth_RNG.Cells(i, 1) = Application.Sum(YTDCYAll_RNG.Rows(i)) - Application.Sum(TotalCYMonth_RNG.Rows(i)) - Application.Sum(MTDCYMonth_RNG.Rows(i))
Next i
What I can not manage to do is instead of making a Sum I want to write a formula in each cell of the range.....like 我无法管理的是代替求和,我想在范围的每个单元格中写一个公式。
Range("BX11:BX13").Formula = "=SUM(C2:D2)"
where "BX11:BX13"
must be replaced by the calculated range LastCYMonth_RNG
in a programmatic manner......no clue about the syntax or mistake a might make. Range("BX11:BX13").Formula = "=SUM(C2:D2)"
,其中"BX11:BX13"
必须以编程方式替换为计算出的范围LastCYMonth_RNG
…… LastCYMonth_RNG
语法或一个可能犯的错误。
Thanks a lot. 非常感谢。
Stefano 斯特凡诺
Here's a small working sample which should show you how to go about it: 这是一个小的工作示例,应该向您展示如何进行操作:
Sub test()
Dim LastCYMonth_RNG As Range
Set LastCYMonth_RNG = Range("B2:c4")
LastCYMonth_RNG.Formula = "=sum(a1:a5)"
End Sub
1) make sure your LastCYMonth_RNG
variable is actually declared as a data type named RANGE
. 1)确保您的
LastCYMonth_RNG
变量实际上已声明为名为RANGE
的数据类型。
2) Set your range to whatever you need (in my test : B2:C4
, in your sample code, BX11:BX13
.. or whatever else you calculated. If you cannot calculate BX11:BX13
, but rather end up with: ( row 11, col 76 )
: ( row 13, col 76 )
, you can use the worksheet function ADDRESS
to build it. [edit] correction - just use CELLS
, not ADDRESS
... [/edit] 2)将范围设置为所需的范围(在我的测试中:
B2:C4
,在示例代码中为BX11:BX13
..或您计算出的任何其他值。如果无法计算BX11:BX13
,而是结束于:( ( row 11, col 76 )
:( ( row 13, col 76 )
,您可以使用工作表函数ADDRESS
进行构建。[edit]更正-仅使用CELLS
而不是ADDRESS
... [/ edit]
3) Once you have that, you can just stuff it into your Formula call. 3)一旦有了这些,就可以将其塞入公式调用中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.