# Autosum doesn't work

#### bibbyd01

##### Board Regular
Sorry for the torrent of questions today, I have lots and lots of things I'm learning at the moment!

I've created a simple macro to add an autosum to the bottom of a dynamic table. The table is created from other parts of the macro, and can vary in size.

I've created a variable that counts how many rows, but I'm stuck on adding it into creating the sum. This is what I've got so far

Dim rowsamount As Integer
Range("A2").Select
rowsamount = Range(Selection, Selection.End(xlDown)).Count
Range("B2").Select
ActiveCell.End(xlDown).Offset(2, 0).Select
Application.Sum (Range(-rowsamount, 0))

I also tried swapping the last line for ActiveCell.FormulaR1C1 = "=SUM(R[-rowsamount]C:R[-2]C) Based on recording an excel macro

How can I get it to sum correctly?

### Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I'm not sure what range you are trying to sum. Here's how to sum from A2 to the last filled row in column A.

Code:
``````Sub Asum()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LR + 1).Value = WorksheetFunction.Sum(Range("A2:A" & LR))
End Sub``````

if column A try

Code:
``````Sub sum1()

lastrow = Worksheets("Sheet1").Range("A65536").End(xlUp).Row

sumcell = "A" & lastrow + 1
rng = "A2:A" & lastrow
Range(sumcell).Value = "=SUM(" & rng & ")"

End Sub``````

Column A contains a list of people, including a row for the headings. After the end of the list there is a row space, then a row for the totals, then under the totals is some more data.

The reason I'm needing to specify the range that it's summing on is because if I used either a column letter(or reference) or xlUp then it will include the additional data below the totals.

So, I want it to specify a range to sum before selecting the cell that the data will appear into.

Try

Code:
``````Sub Asum()
Dim LR As Long
LR = Range("A2").End(xlDown).Row
Range("B" & LR + 1).Value = WorksheetFunction.Sum(Range("B2:B" & LR))
End Sub``````

Thanks, works excellently, and more importantly, makes sense!!

Replies
1
Views
342
Replies
2
Views
240
Replies
6
Views
525
Replies
5
Views
474
Replies
4
Views
272

1,218,625
Messages
6,143,573
Members
450,493
Latest member
Woejeber

### 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.

### Which adblocker are you using?

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

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