How to Sum a range between cells that have a specific string

louisedp

New Member
Joined
Dec 22, 2013
Messages
21
Hi,

In the below Example the value of cell M2: 387 is arrived by using the formula =SUM(L3:L9)
and M10: 418 is arrived by using the formula =SUM(L11:L17)

i.e , Need to sum the integers in-between two "Day-IN"s

Challange:
There is more than 1000 records(more than 500 "Day-IN"s) for which I cannot manually use Sum for the cells in-between "Day-IN"s
1</SPAN>L </SPAN>M</SPAN>
2</SPAN>Day-IN387</SPAN>
3</SPAN>112</SPAN>
4</SPAN>-65</SPAN>
5</SPAN>90</SPAN>
6</SPAN>-4</SPAN>
7</SPAN>115</SPAN>
8</SPAN>-47</SPAN>
9</SPAN>70</SPAN>
10</SPAN>Day-IN</SPAN>418</SPAN>
11</SPAN>128</SPAN>
12</SPAN>-56</SPAN>
13</SPAN>144</SPAN>
14</SPAN>-5</SPAN>
15</SPAN>64</SPAN>
16</SPAN>-38</SPAN>
17</SPAN>82</SPAN>
18</SPAN>Day-IN</SPAN>383</SPAN>
19</SPAN>86</SPAN>
20</SPAN>-0</SPAN>
21</SPAN>1</SPAN>
22</SPAN>-51</SPAN>
23</SPAN>95</SPAN>
24</SPAN>-6</SPAN>
25</SPAN>130</SPAN>
26</SPAN>-56</SPAN>
27</SPAN>71</SPAN>
28</SPAN>Day-IN</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>


Refer Image : View image: Sum between cells


Someone, Kindly shed some light pls...
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
This is one way :-
But the results do not match Yours ????
Code:
[COLOR="Navy"]Sub[/COLOR] MG21Jul12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, sum [COLOR="Navy"]As[/COLOR] Double, Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Dn.Value = "Day-IN" [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Not Temp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] Temp.Offset(, 1) = sum
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
        sum = 0
    [COLOR="Navy"]Else[/COLOR]
        sum = sum + Dn.Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mick,

Thank you for your reply....

But It would be great if I achieve it using some sort of formula(Index,Reference)...instead of a macro....

This is one way :-
But the results do not match Yours ????
Code:
[COLOR=navy]Sub[/COLOR] MG21Jul12
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, sum [COLOR=navy]As[/COLOR] Double, Temp [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]If[/COLOR] Dn.Value = "Day-IN" [COLOR=navy]Then[/COLOR]
        [COLOR=navy]If[/COLOR] Not Temp [COLOR=navy]Is[/COLOR] Nothing [COLOR=navy]Then[/COLOR] Temp.Offset(, 1) = sum
        [COLOR=navy]Set[/COLOR] Temp = Dn
        sum = 0
    [COLOR=navy]Else[/COLOR]
        sum = sum + Dn.Value
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi.

Not sure I understand. The sum of those values (in L3:L9) is nowhere near 387. Neither is the sum of the values in L11:L17 anywhere near 418.

Regards
 
Upvote 0
Hi XOR,

Thank you for your reply...

It's a nice catch... Even I just noticed.... Just I prepared the example table with dummy data... I failed to notice that the Negative Integers were stored as text, so the summation was wrong...

But my requirement is the same... I need the summation of the cells in between two "Day-IN" cells...

1</SPAN>L </SPAN>M</SPAN>
2</SPAN>Day-IN271</SPAN>
3</SPAN>112</SPAN>
4</SPAN>-65</SPAN>
5</SPAN>90</SPAN>
6</SPAN>-4</SPAN>
7</SPAN>115</SPAN>
8</SPAN>-47</SPAN>
9</SPAN>70</SPAN>
10</SPAN>Day-IN</SPAN>319</SPAN>
11</SPAN>128</SPAN>
12</SPAN>-56</SPAN>
13</SPAN>144</SPAN>
14</SPAN>-5</SPAN>
15</SPAN>64</SPAN>
16</SPAN>-38</SPAN>
17</SPAN>82</SPAN>
18</SPAN>Day-IN</SPAN>270</SPAN>
19</SPAN>86</SPAN>
20</SPAN>-0</SPAN>
21</SPAN>1</SPAN>
22</SPAN>-51</SPAN>
23</SPAN>95</SPAN>
24</SPAN>-6</SPAN>
25</SPAN>130</SPAN>
26</SPAN>-56</SPAN>
27</SPAN>71</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL><COL></COLGROUP>


Hi.

Not sure I understand. The sum of those values (in L3:L9) is nowhere near 387. Neither is the sum of the values in L11:L17 anywhere near 418.

Regards
 
Upvote 0
Thanks. But how do you arrive at a result in cell M18? There are no more entries of "Day-IN" in column L.

Regards
 
Upvote 0
Xor,

The Value of cell L28 is "Day-IN"...

It goes-on.... The collumn L has more than 1000 records... with "Day-In" in-between...

Hope u understand...



Thanks. But how do you arrive at a result in cell M18? There are no more entries of "Day-IN" in column L.

Regards
 
Upvote 0
So I'll take it by that the last entry in the column is always "Day-IN".

In M2:

=IF(L2="Day-IN",SUM(L3:INDEX(L3:L$1048576,MATCH("Day-IN",L3:L$1048576,0)-1)),"")

Copy down as required.

Regards
 
Upvote 0
Thanks a Million Xor....

It's working like a charm..... :)

Basically I'm trying to calculate the Total-Time Spent by an employee Inside and Outside a Datacenter, using the Swipe Report...

System Date Time</SPAN>Terminal</SPAN>
06/Apr/15 03:10 PM</SPAN>IN</SPAN>
06/Apr/15 05:03 PM</SPAN>OUT</SPAN>
06/Apr/15 06:09 PM</SPAN>IN</SPAN>
06/Apr/15 07:39 PM</SPAN>OUT</SPAN>
06/Apr/15 07:44 PM</SPAN>IN</SPAN>
06/Apr/15 09:39 PM</SPAN>OUT</SPAN>
06/Apr/15 10:26 PM</SPAN>IN</SPAN>
06/Apr/15 11:37 PM</SPAN>OUT</SPAN>
07/Apr/15 02:56 PM</SPAN>IN</SPAN>
07/Apr/15 05:05 PM</SPAN>OUT</SPAN>
07/Apr/15 06:02 PM</SPAN>IN</SPAN>
07/Apr/15 08:26 PM</SPAN>OUT</SPAN>
07/Apr/15 08:31 PM</SPAN>IN</SPAN>
07/Apr/15 09:36 PM</SPAN>OUT</SPAN>
07/Apr/15 10:14 PM</SPAN>IN</SPAN>
07/Apr/15 11:36 PM</SPAN>OUT</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL></COLGROUP>



The Challenge is, there are 3 shifts:

6am - 3pm
2pm - 11pm
10pm - 7am (the next day)...

It is very difficult to calculate for the rolling days... Do you have any idea....
 
Upvote 0
I don't know what you mean, I'm afraid, since you haven't explained or, even better, given any expected results for this new scenario.

Regards
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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