[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Excel 2010 - VBA  - How to most efficiently add up columns in a worksheet

Posted on 2014-08-03
Medium Priority
309 Views
I have attached an Excel workbook that I would like to have two short columns summed (EAST AND WEST).
Here is a picture in case you don't want to open the actual workbook.

What is the most efficient way to code this.

I used this and it worked but I suspect there might be a more efficient way.
Option Explicit

Dim SalesEast As Currency
Dim SalesWest As Currency
Dim Sum As Currency
Dim CustomerName As String
Dim Price As Currency
Dim OrderDate As Date

Sub UsingOperators()

Range("b9").Activate
ActiveCell.FormulaR1C1 = "=sum(r[-4]c:r[-1]c)"
Range("c9").Activate
ActiveCell.FormulaR1C1 = "=sum(r[-4]c:r[-1]c)"

SalesEast = Cells(9, 2).Value
SalesWest = Cells(9, 3).Value
Sum = SalesEast + SalesWest
Cells(11, 4).Value = Sum

End Sub
question.xlsm
0
Question by:brothertruffle880

LVL 12

Expert Comment

ID: 40238188
Hi brothertruffle880,

Why are you using a macro to do this?
Why don't you just put this formula in cell D11:
=B9+C9
0

LVL 27

Accepted Solution

Glenn Ray earned 2000 total points
ID: 40238265
I agree that inserting a formula in D11 is simplest, but if you want to insert that formula in VBA, here's your code with a simpler command to do that:
Sub UsingOperators()
Range("B9").Formula = "=SUM(B5:B8)"
Range("C9").Formula = "=SUM(C5:C8)"
Range("D11").Formula = "=B9+C9"
End Sub

However, if your list of products varies, this code (nor the original code) would be helpful; a more dynamic set of formulas would be needed.

Regards,
-Glenn
0

LVL 34

Expert Comment

ID: 40238750
This is, I suspect, a summary of a larger data set.

How is the data organised?

You might be able to use a Pivot Table on the source data to give you the figures you require.

Thanks
Rob H
0

Author Comment

ID: 40240012
HI All:
I'm experimenting with VBA.
I can definitely copy a function  and find easier ways to get this job done  But my purpose in posting the question was to explore alternative ways of getting something done in VBA.
I'm looking for alternatives to my solution using VBA code.
0

LVL 12

Expert Comment

ID: 40240217
Hi brothertruffle880,

That's the kind of info that it is good to have clearly stated in the original post, because when I read it, it looked as if you wanted efficiency, and you thought you needed to VBA to do it.  So please bear that in mind for future questions, so experts don't waste time going down fruitless tracks.

Given your requirements, what is wrong with Glenn's solution?  Looks pretty efficient (and concise) to me.

tel2
0

## Featured Post

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…
###### Suggested Courses
Course of the Month20 days, 12 hours left to enroll