Sum up if 2 conditions are met

coolshawn

New Member
Joined
Oct 24, 2017
Messages
4
Hi all,

I need help to write codes for the following situation.

Fix rebar
Up to 12mm diameter. 37,060.00
16mm diameter. 224,740.00
20mm diameter.
25mm diameter.
32mm diameter. 7,140.00
Concrete Grade

<colgroup><col><col></colgroup><tbody>
</tbody>

I need to sum up all values between fix rebar and concrete grade. First I need to find where fix rebar is, and then look for concrete grade. Once I know their positions, I need to find the sum of all items in between them. I have several hundreds of them in my database, and need to find sum for all of them. Anyone can help? I really appreciate it. Thanks in advance.

shawn
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi,

Assume that column A contains text, including Fix rebar and concrete grade
And column B contains the values.

C1 =ADDRESS(SUMPRODUCT((A1:A7="Fix rebar")*ROW(A1:A7)),ROW(A2),4)
D1 =ADDRESS(SUMPRODUCT((A1:A7="Concrete Grade")*ROW(A1:A7)),ROW(A2),4)
E1 to sum between them =SUM(INDIRECT(C1):INDIRECT(D1))
 
Upvote 0
hi admiral,

thank you for quick response. It worked for the 1st instance. but when i applied to the rest of the table, it did not produce results as I expected. It only picked up the first correct solution and carried it to the rest of the table. I would not dare to respond earlier because I did not know enough about address and sumproduct functions. I tried to expand the range you gave from A1:A7 to A1:A4890 but did not solve my problem. Can you please help to include the rest of the table? Thank you.
 
Upvote 0
Hi Aladin,

Thank you for the quick response. The formula worked, but only for the first instance. When I expanded it to include the whole range, it did not produce the expected results.

I am attaching the expanded sample of my database to make my problem clearer.

IDTASKCOST
1
Fix rebar
2
Up to 12mm
5,474.00
3 16mm
4
20mm
5 25mm
7,038.00
6 32mm
99,314.00
7
40mm
8 Concrete Grade
110,875.13
9 Cutting off piles
18,504.00
10 Carry out MLT
165,210.00
11
subsequent MLT
396,450.00
12
Carried to Collection
963,113.37
13 Carry out PDA
22,900.00
14 750mm Diameter Bored Pile
15
Allow for mobilization
51,540.00
16 Drill 750 mm diameter holes
99,905.40
17 Drilled 750 mm raked holes
94,937.80
18 12 mm thick casing
19 Fix rebar
20
Up to 12mm
14,416.00
23
25mm
24 32mm
119,646.00
25 40mm
26 Concrete Grade

<colgroup><col><col><col></colgroup><tbody>
</tbody>

As it is shown in the sample, fix rebar appears twice - ID #1 and #1 9. The same thing with concrete grade. It is also listed twice - ID #8 and #26 .

So my problem is to find the sum of all values between these 2 conditions. Look for fix rebar and look for concrete grade. Then find the sum in between those two. And then continue down the table and look for the same conditions. Now in between those two conditions, I have many other rows with values, but I am not interested to find their sum. I am only looking for the sum whatever between fix rebar and concrete grade. The number of rows between those 2 conditions are not fixed. In the first instance, there are 6 rows to add, but in the second instance, only 4.

I hope I am making my problem more clear. Thank you for the help so far, and I hope someone can help me solve this problem.

Thank you.

shawn
 
Upvote 0
For the 1st instance, 111,826 (5,474+7,038+99,314) --between ID 1 and 8
For the 2nd instance, 134,062 (14,416+119,646) --between ID 19 and 26.

Let A:C house the data, the row with headers included.

In E1 enter:

=COUNTIFS(B:B,"fix rebar")

In E2 control+shift+enter, not just enter, and copy down:

=IF(ROWS($E$2:E2)>$E$1,"",SUM(INDEX($C$2:$C$25,SMALL(IF($B$2:$B$25="fix rebar",ROW($B$2:$B$25)-ROW($B$2)+1),ROWS($E$2:E2))):INDEX($C$2:$C$25,SMALL(IF($B$2:$B$25="concrete grade",ROW($B$2:$B$25)-ROW($B$2)+1),ROWS($E$2:E2)))))

Note that this set up assumes correct pairing, that is, fix rebar...concrete grade...fix rebar...concrete grade, etc., not sequences like ...concrete grade...fix rebar...fix rebar..., etc.
 
Upvote 0
Another option : Ctrl+Shift+Enter

C1 =SUM(INDEX($B$2:$B$25,SMALL(IF($A$2:$A$25="Fix rebar",ROW($A$2:$A$25),""),ROW(A1))-1):INDEX($B$2:$B$25,SMALL(IF($A$2:$A$25="Concrete Grade",ROW($A$2:$A$25),""),ROW(A1))-2))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,336
Members
449,443
Latest member
Chrissy_M

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top