Solved

VBA to sum a column

Posted on 2016-10-07
13
47 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 33

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 18

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 26

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

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 26

Expert Comment

by:ProfessorJimJam
ID: 41833415
since you have the data on L column, so i updated the example file.
EE.xlsx
0
 

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 18

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 26

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 33

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 18

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

840 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