Autosum with vba

iffi

Board Regular
Joined
Jun 5, 2011
Messages
59
Office Version
  1. 2019
Platform
  1. Windows
starting point is fixed that is D1 but ending point is not fixed, it depends on the data, it can be any range, i want autosum after one row where the data ends, example From D1 and if data ends till D80 then autosum should be in D82, and if data ends till D120 then autosum should be in D122.
what could be the code for this ? Thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Something like this perhaps?
Code:
Range("D" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=SUM(R2C:R[-2]C)"

Note this assumes you would want to sum D2 to the last row of data.
 
Upvote 0
Try

Code:
Sub Asum()
Dim LR As Long
LR = Range("D" & Rows.Count).End(xlUp).Row
Range("D" & LR + 2).Formula = "=SUM(D1:D" & LR & ")"
End Sub
 
Upvote 0
Oops, thought you had a header.:oops:

Change R2C to R1C.
 
Upvote 0
and try this
Code:
Sub AuotSum()
Dim LR As Long
LR = Range("d" & Rows.Count).End(xlUp).Row
Range("d" & LR + 2).Value = Application.WorksheetFunction.Sum(Range("D1:D" & LR))
End Sub
 
Upvote 0
Thanks VoG and Norie, Both codes r working great. Thanks
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

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