Autosum doesn't work

bibbyd01

Board Regular
Joined
Sep 10, 2008
Messages
113
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

shyrath

Active Member
Joined
Jun 28, 2005
Messages
483
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
 

bibbyd01

Board Regular
Joined
Sep 10, 2008
Messages
113
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.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,550
Messages
5,636,962
Members
416,952
Latest member
prakashkumar

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
Top