简体   繁体   English

如何在Excel中的sumproduct中动态选择单元格范围

[英]How to select cell range dynamically in sumproduct in excel

I am trying to use Sumproduct dynamically on a sheet that will be updated regularly. 我试图在会定期更新的工作表上动态使用Sumproduct。

My motive is to not manually select the columns ( ie not to use the shift key and down key to select the range A2:A6 as seen in the attached snapshot) 我的动机是不要手动选择列(即,不使用shift键和向下键来选择范围A2:A6,如所附快照所示)

I want to have the formula "=SUMPRODUCT(--(A2:A6="TX"))" pick up the A2:A6 dynamically since my data in worksheet say A is everchanging. 我想让公式“ = SUMPRODUCT(-(A2:A6 =” TX“))”动态获取A2:A6,因为我在工作表中的数据说A在不断变化。 So that when the data has grown to A8, it can be somehow picked up dynamically than manually using the keys. 这样,当数据增长到A8时,可以通过某种方式动态地获取这些数据,而不是使用键手动获取。

Can this be done? 能做到吗? Please let me know if my question is not clear. 如果我的问题不清楚,请告诉我。

Screenshot of my xls 我的xls的屏幕截图

Regards SM 关于SM

Yes, you could define the range dynamically. 是的,您可以动态定义范围。 However, given your current formula, I see no reason why you should not switch to COUNTIF here, ie: 但是,考虑到您当前的公式,我认为没有理由不应该在此处切换到COUNTIF ,即:

=COUNTIF(A:A,"TX")

a function for which the use of entire column referencing ( A:A here) has no detriment to performance (which is not at all the case with SUMPRODUCT ). 一个使用整个列引用(此处为A:A )不会影响性能的SUMPRODUCT完全不是这种情况)。

Regards 问候

Shitty recommendations guy, here. 卑鄙的建议家伙,在这里。

Can you insert a new line above all in your sheet? 您可以在工作表中首先插入新行吗? You could put it really wherever you want and you can even hide it, the first line just works for me. 您可以将它放在您想要的任何地方,甚至可以将其隐藏起来,第一行对我有用。

If you can, just make it an "count.values" function on the entire column with an appropriated subtraction to adjust the count to the right length of your data. 如果可以的话,只需在整个列上将其设置为“ count.values”函数,然后适当减去即可将计数调整为正确的数据长度。 Eg Removing this auxiliary cell value plus a header would be "=count.values(A:A)-2". 例如,删除此辅助单元格值加上标题将为“ = count.values(A:A)-2”。

Later, on your VBA, you could retrieve this value to a variable and concatenate it to your main function. 稍后,在您的VBA上,您可以将该值检索到变量并将其连接到您的主函数。 Lets say you made the auxiliary on cell A1 (coordinates 1, 1) 假设您在单元格A1上做了辅助(坐标1、1)

Dim auxLine As Integer auxLine = cells(1, 1).value whatever.formular1c1 = "=SUMPRODUCT(--(A2:A" & auxLine & ="TX"))"

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

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