vba sum values in row for several rows

Gtabtr1

New Member
Joined
Feb 15, 2017
Messages
39
Hi all,
Addition has never been so frustrating.

I have many, many rows of numbers across several columns. The number of columns and rows will both vary. Values will begin in column B in each row. Would like to avoid loops, too many rows.

Dim lcol as Long
Dim fcol as Long
Dim rrow as Long
lcol = cells(1,columns.count).End(xlToLeft).Column
fcol = (lcol + 1)
rrow=Range("A" & Rows.Count).End(xUp).Row

With Range(Cells(2,fcol),Cells(rrow.fcol)) 'this is the range for results, tested and ok.
.formula = "=sum(" & Cells(2,2).Address & ":" & Cells(2,lcol).Address & ") "
.Value = .Value
End With

I get the answer for the first row in all rows in range.
Help Please.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try
Code:
Dim lcol As Long
Dim fcol As Long
Dim rrow As Long
lcol = Cells(1, Columns.Count).End(xlToLeft).Column
fcol = (lcol + 1)
rrow = Range("A" & Rows.Count).End(xlUp).Row

With Range(Cells(2, fcol), Cells(rrow, fcol)) 'this is the range for results, tested and ok.
.Formula = "=sum(" & Cells(2, 2).Address(0, 0) & ":" & Cells(2, lcol).Address(0, 0) & ") "
.Value = .Value
End With
 
Upvote 0
Thank you Fluff. I appreciate your help!!!!! Couldn't do what I do without the board's help.
Have a great weekend.
 
Upvote 0
Have a good weekend yourself.
For reference a slightly easier way would be
Code:
With Range(Cells(2, fcol), Cells(rrow, fcol)) 'this is the range for results, tested and ok.
.FormulaR1C1 = "=sum(rc2,rc[-1])"
.Value = .Value
End With
 
Upvote 0
Have a good weekend yourself.
For reference a slightly easier way would be
Code:
With Range(Cells(2, fcol), Cells(rrow, fcol)) 'this is the range for results, tested and ok.
.FormulaR1C1 = "=sum(rc2,rc[-1])"
.Value = .Value
End With
You can also do it directly without placing the formula in the cells only to have to then equate values with themselves...
Code:
Dim LastCol As Long, OutCol As Long, LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
With Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
  .Value = Evaluate("IF({1},SUBTOTAL(9,OFFSET(B1,ROW(A1:A" & LastRow - 1 & "),0,1," & LastCol - 1 & ")))")
End With
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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