简体   繁体   English

引用单元格以求和

[英]Referencing a cell for range in an excel sum

I am producing a spreadsheet in which some costs are being assigned either 0 or 1 based on a condition, as below; 我正在制作一个电子表格,其中根据条件将一些成本分配为0或1,如下所示;

Range("V12").Select
ActiveCell.Formula = "=IF(T12 < 50000000, 1, 0)"
Range("V12").Select
Selection.AutoFill Destination:=Range("V12:V511"), Type:=xlFillDefault
Range("V12:V511").Select
Calculate

The line Range("V12:V511").Select starts at V12 but I need the final cell to change based on the number placed in cell U3. Range("V12:V511").Select从V12开始,但是我需要根据单元格U3中放置的数字来更改最后一个单元格。 I'm not sure how to do this, but in essence I want to be able to write something along the lines of Range("V12:VRange("U3").Value - 1").Select so that the value of cell U3 is used in this range. 我不确定如何执行此操作,但是本质上我希望能够按照Range("V12:VRange("U3").Value - 1").Select方式编写某些内容Range("V12:VRange("U3").Value - 1").Select以便单元格的值在此范围内使用U3。

Also, I want to sum these 0s and 1s over that range but am not sure how to dynamically change the limits of a sum. 另外,我想对这个范围内的这些0和1求和,但不确定如何动态更改总和的限制。

Range expects a string so we just need to concatenate the value from U3 with the rest of the address string. Range需要一个字符串,因此我们只需要将U3中的值与地址字符串的其余部分连接起来。

All those line can be replaced with: 所有这些行都可以替换为:

ActiveSheet.Range("V12:V" & ActiveSheet.Range("U3")-1).Formula = "=IF(T12 < 50000000, 1, 0)"

Or if your prefer R1C1 notation: 或者,如果您更喜欢R1C1表示法:

ActiveSheet.Range("V12:V" & ActiveSheet.Range("U3")-1).FormulaR1C1 = "=IF(RC[2] < 50000000, 1, 0)"

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

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