简体   繁体   中英

LastRow in excel function changes based on value in cell without using vba

I have the following function in excel:

=SUMIF($H$2:$H$500;CONCATENATE(N$1;$K$1;$K2;$M$1;$M2);$C$2:$C$500)

In this instance it's in column N row 2. I have a macro that pulls this function down the column.

In this function it considers everything from cell H2 to H500

$H$2:$H$500

and C2 to C500

$C$2:$C$500

C500 & H500 = LastRow, I manually entered this number, but when the amount of entries exceed this number, they do not get counted and the outcome is incorrect.

I have a cell R2 that contains a number, 1, and it will change according to the LastRow I have figured out from another file. This will update automatically.

I want the number 500, which is now a static number that I typed, to change according to the number (value) in cell R2. To make it more flexible.

If the value in cell R2 changes from 1 to 300, then the number 500 in this example, needs to change to this 300 from cell R2. I know that when the change to this function is made that it would not be 500 but 1, because the number in cell R2 is 1 and not 500.

It would be something like:

=SUMIF($H$2:$H$**R2**;CONCATENATE(N$1;$K$1;$K2;$M$1;$M2);$C$2:$C$**R2**)

I tried selecting the cell R2 when I selected the $500 in the function but it would instead look at the range between H2 and R2 as a selection, but this is not what I want.

I want it to look still at only the H column, just more or less rows, which is defined by the value in R2.

I hope somebody has a solution for this.

My thanks in advance.

在这种情况下,您可以使用INDIRECT()建立地址。

=SUMIF(INDIRECT("$H$2:$H$" & TEXT($R$2, "#####"));CONCATENATE(N$1;$K$1;$K2;$M$1;$M2);INDIRECT("$H$2:$H$" & TEXT($R$2, "#####"))

You can use the OFFSET function to give you what you wish. OFFSET returns a range based on the parameters you've chosen. In this case you can supply OFFSET with the number of rows to return based on your cell R2 .

There's a good example here from Microsoft so I won't repeat it here.

Your formula then becomes:

=SUMIF(OFFSET(Sheet1!$H$2;0;0;Sheet1!$R$2-1;1);CONCATENATE(N$1;$K$1;$K2;$M$1;$M2);OFFSET(Sheet1!$C$2;0;0;Sheet1!$R$2-1;1))

Bear in mind that you are still hard-coding the start of the rows to H2 and C2 respectively, therefore if these move, you will need to ensure that the adjustment (ie the -1 in the part Sheet1!$R$2-1 ) is correct for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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