Solved

VBA to sum a column

Posted on 2016-10-07
13
43 Views
Last Modified: 2016-10-07
Need an experts help please to sum a column.

Column L will have various amounts of data each day. Today 3000 items tomorrow 10000 items following day 2000 items.

I need to go to the end on the data offset 1 and then sum sum the column

Would appreciate some help.
0
Comment
Question by:Jagwarman
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41833399
Is your formula in the same column as the data?

If not, just use the whole column:

=SUM(L:L)
0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41833406
If you want VBA then try this

Option Explicit

Sub SumL()
Dim rRng As Range

Set rRng = Range(Cells(1, 12), Cells(Rows.Count, 12).End(xlUp))

rRng.Offset(1).Formula = Application.WorksheetFunction.Sum(rRng)
End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 41833411
you can use the dynamic range inside the formula

like this

lets say your daya starts from A2 to the last cell that has data

use this formula =SUM(INDIRECT("A2:A"&MATCH(2,INDEX(1/(A:A<>""),))))

see atached.
EE.xlsx
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 41833412
Hi,

pls try

Sub SumColL()

Set myRng = Range(Cells(1, 12), Cells(Rows.Count, 12).End(xlUp))
myRng.Offset(myRng.Rows.Count).Resize(1).Formula = "=SUM(" & myRng.Address(0, 0) & ")"
End Sub

Open in new window

Regards
1
 

Author Comment

by:Jagwarman
ID: 41833414
Hi Roy Cox.

what your code is doing is changing every cell from L2 down to the same amount instead of adding them to come to a total

£118.16
£118.16
£118.16
£118.16
£118.16
£118.16
£118.16
£118.16
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 41833415
since you have the data on L column, so i updated the example file.
EE.xlsx
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Jagwarman
ID: 41833418
Rob Henson & Prof JimJam

Because I need the total to be in the same column [in the first cell after the last item] I cannot use your formula
0
 
LVL 17

Assisted Solution

by:Roy_Cox
Roy_Cox earned 250 total points
ID: 41833427
Try this amendment

Option Explicit

Sub SumL()
Dim rRng As Range

Set rRng = Range(Cells(1, 12), Cells(Rows.Count, 12).End(xlUp))
Cells(Rows.Count, 12).End(xlUp).Offset(1).Formula = Application.WorksheetFunction.Sum(rRng)
End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 41833452
@Jagwarman

Rob Henson & Prof JimJam

Because I need the total to be in the same column [in the first cell after the last item] I cannot use your formula

in that case then Rgonzo's macro is best fit for you

https://www.experts-exchange.com/questions/28974978/VBA-to-sum-a-column.html?anchor=a41833418#a41833412
0
 

Author Closing Comment

by:Jagwarman
ID: 41833457
Thanks Experts have a great weekend.
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41833463
Are you doing this update frequently?

If not, why use a macro at all?  Select L2 and press End and then Down arrow and down arrow again, then click on the Autosum button on the far right of the home ribbon, Sigma symbol. This will populate the cell with a sum of everything above. Press enter to confirm.

No VBA invoked so no worry about the Undo history being lost.

Thanks
Rob H
0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41833490
Pleased to help
0
 

Author Comment

by:Jagwarman
ID: 41833510
Hi Rob,

Yes I am familiar with your proposal but as always with my requests they are part of a much bigger report that uses VBA to do lots of splitting, merging, formulas etc.

Regards
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now