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

Posted on 2014-08-03
Last Modified: 2014-08-05
I have attached an Excel workbook that I would like to have two short columns summed (EAST AND WEST).
err  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()

    ActiveCell.FormulaR1C1 = "=sum(r[-4]c:r[-1]c)"
    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

Open in new window

Question by:brothertruffle880
    LVL 11

    Expert Comment

    Hi brothertruffle880,

    Why are you using a macro to do this?
    Why don't you just put this formula in cell D11:
    LVL 27

    Accepted Solution

    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

    Open in new window

    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.

    LVL 31

    Expert Comment

    by:Rob Henson
    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.

    Rob H

    Author Comment

    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.
    LVL 11

    Expert Comment

    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.


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    I've recently been in need of an Excel macro that could add a letter before the text on multiple cells in an Excel document. My English is as it is, so I will try explain what it does diffrently. If you have an excel document with 2000 rows an…
    Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

    728 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

    14 Experts available now in Live!

    Get 1:1 Help Now