简体   繁体   中英

Using Excel formula to SUM a number based on the letter infront

I have a spreadsheet with a couple of cells with the following input:

V4 V5 V2 V3 V5 V7

I am trying to tell Excel to check the value infront, and see if its a V. If this is TRUE, count the value next to it. This has to be done in a loop until it has passed through the range and summed all of the values.

This formula gets me nearly there.. but still no workable output.

=SUMPRODUCT(--(LEFT(A1:AO1,1)="V")*SUM(RIGHT(A1:AO1,1)))

VBA is not a option.

You were very close, use the array formula :

=SUMPRODUCT(--(LEFT(A1:AO1,1)="V"),IFERROR(--RIGHT(A1:AO1,1),0))

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

EDIT#1:

As mentioned in the Comment by guitarthrower , this will work if there is more than one digit following the V:

=SUMPRODUCT(--(LEFT(A1:AO1,1)="V"),IFERROR(--RIGHT(A1:AO1,LEN(A1:AO1)-1),0))

EDIT#2:

If you are using a version of Excel before 2007, then use this array formula:

=SUMPRODUCT(--(LEFT(A1:AO1,1)="V"),IF(ISERROR(--RIGHT(A1:AO1,LEN(A1:AO1)-1)),0,--RIGHT(A1:AO1,LEN(A1:AO1)-1)))

Assuming the value next to the V is one digit long, you can use the following IF statement in excel to check if there is indeed a V , then assign the cell the value next to the V , or a zero.

=IF(LEFT(A1; 1)="V"; RIGHT(A1;1);0)

Afterwards, it's up to you to tweak it a bit.

Don't fight excel, the obvious solution is to do:

  =IF(LEFT(A1,1) = "v", RIGHT(A1,1), 0)

on a column (you can hide it) and then sum it normally.

The other answers all require a helper column, but your questions seems to suggest that is what you are trying to avoid. try the formula below (abjust range as necessary):

=SUMPRODUCT(--(LEFT(A1:A8,1)="V"),VALUE(RIGHT(A1:A8,1)))

Please bear in mind that this will ONLY work if the number after the letter is always a single digit

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