Question:
VBA Excel FOR loop sum?
anonymous
2012-10-06 19:23:05 UTC
Hello
i want to calculate and display simultaneously the sum of all these columns in each row.

______col 1 2 3 4 5 6 7
___row 1 x x x x x x display sum of X
_______2 y y y y y y display sum of y
_______3

i think its
for rows 1 to 2
for cols 1 to 6

sum = sum + sum


next cols
next rows

where would i insert vb to display sum of each row ?

thankyou
Three answers:
Doreen
2012-10-07 01:53:54 UTC
I assume you recognize that this can be done very easily without VBA, but you just want to see how to use FOR NEXT loops to do the job. Here is the code:



Sub Sums()

For r = 1 To 2

Sum = 0

For c = 1 To 6

Sum = Sum + Cells(r, c)

Next c

Cells(r, 7) = Sum

Next r

End Sub
srikant
2016-12-19 00:35:12 UTC
Fasten your seat belts We would desire to have some assumptions: a million- you have that record in column A commencing from A4 2- you have cellular C4 that have the sum of all column A like this =SUM(A:A) 3- and you have the 11s as finished cost of each cellular which potential, you do not have this in cellular A4 for occasion 45113 yet you merely have 11 4- And final, you have that sheet named as Sheet1 Then use this macro Sub Conver11s_2_1s() Dim StD as variety, StS as variety Set Std= ThisWorkbook.Worksheets( "Sheet1").variety( "A4") Set StS= ThisWorkbook.Worksheets( "Sheet1").variety( "C4") X1= 0 St1= Std.Offset( X1).cost Do until eventually St1="" If Val( StS.cost) > 50 then go out Do if Val(St1) = 11 then Std.Offset( X1).cost =a million X1= X1 +a million St1= Std.Offset( X1).cost Loop end Sub attempt it, it is working and remember to have a good time with my profile, VBAXLMan is returned
AJ
2012-10-06 19:47:00 UTC
Real easy, start recording a new Macro, select the cell where you want the sum and click on the autosum button.



Stop macro and go edit macro and you can see exactly the VBA code you need.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...