How to sum the column value based on other column value?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
Hi,

I have following data and how to write VBA to get 39 and 22 and so on:
Once we see 1 in Column A. Strats adding next number in column B until next 1 in column 1.

Exanple: 39 = 5+5+6+7+3+8+3+2
22 = 9+6+3+4

Col-A Col-B
139
25
35
46
57
63
78
83
92
122
29
36
43
54
1

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try this:
Code:
Sub test()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 2))
sumr = 0
For i = lastrow To 1 Step -1
 If inarr(i, 1) = 1 Then
  Cells(i, 2) = sumr
  sumr = 0
 Else
  sumr = sumr + inarr(i, 2)
 End If
Next i


End Sub
 
Upvote 0
Offthelip:
Thank you very much. I really like the approach - NEVER ACCESS THE WORKSHEET N A LOOP.

I APPRECIATE IT.
 
Upvote 0
I am modifying above code if I have more columns like Column B, Column C and many more. In future, I may have many rows and may columns

But it gives me subscript out of error.

Code:
Sub test()
lastrow = Cells(Rows.count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 2))
sumr = 0
sumr2 = 0
For i = lastrow To 1 Step -1
 If inarr(i, 1) = 1 Then
  Cells(i, 2) = sumr
  Cells(i, 3) = sumr2
  sumr = 0
  sumr2 = 0
 Else
  sumr = sumr + inarr(i, 2)
  sumr2 = sumr2 + inarr(i, 3)
 End If
Next i

End Sub
 
Upvote 0
this line of code:
Code:
inarr = Range(Cells(1, 1), Cells(lastrow, 2))
Loads a defined range into the variant array, the range I defined was from cells (1,1) which is A1 to (cells(lastrow,2) which the Lastrow of column B
If you want to use more columns you need to change the Cells(lastrow,2) to cells(lastrow, N) where N is the number of the lastrow that you want to use.
To find the column number of a column in excel there is a very useful function that I use all the time; put the following function in to any cell in the column you want to find the numberr of and it will give the number:

=Column()
 
Upvote 0

Forum statistics

Threads
1,215,573
Messages
6,125,608
Members
449,238
Latest member
wcbyers

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