Return to VBA Code Examples

VBA Sum Function (Ranges, Columns, & More)

This tutorial will show you how to use the Excel Sum function in VBA

The sum function is one of the most widely used Excel functions, and probably the first one that Excel users learn to use. VBA does not actually have an equivalent – a user has to use the built-in Excel function in VBA using the WorkSheetFunction object.

Sum WorksheetFunction

The WorksheetFunction object can be used to call most of the Excel functions that are available within the Insert Function dialog box in Excel. The SUM function is one of them.

vba sum syntax

You are able to have up to 30 arguments in the SUM function. Each of the arguments can also refer to a range of cells.

This example below will add up cells D1 to D9

The example below will add up a range in column D and a range in column F. If you do not type the Application object, it will be assumed.

Notice for a single range of cells you do not have to specify the word ‘Range’ in the formula in front of the cells,  it is assumed by the code. However, if you are using multiple arguments, you do need to do so.

Assigning a Sum result to a Variable

You may want to use the result of your formula elsewhere in code rather than writing it directly back to and Excel Range. If this is the case, you can assign the result to a variable to use later in your code.

vba sum result

Sum a Range Object

You can assign a group of cells to the Range object, and then use that Range object with the WorksheetFunction object.

Sum Multiple Range Objects

Similarly, you can sum multiple Range Objects.

Sum Entire Column or Row

You can also use the Sum function to add up an entire column or an entire row

This procedure below will add up all the numeric cells in column D.

While this procedure below will add up all the numeric cells in Row 9.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Sum an Array

You can also use the WorksheetFunction.Sum to add up values in an array.

Using the SumIf Function

Another worksheet function that can be used is the SUMIF function.

The procedure above will only add up the cells in Range(D2:D10) if the corresponding cell in column C = 150.

vba sumif value

Sum Formula

When you use the WorksheetFunction.SUM to add a sum to a range in your worksheet, a static sum is returned, not a flexible formula. This means that when your figures in Excel change, the value that has been returned by the WorksheetFunction will not change.

vba sum static

In the example above, the procedure TestSum has added up Range(D2:D10) and the result has been put in D11. As you can see in the formula bar, this result is a figure and not a formula.

If any of the values change therefore in the Range(D2:D10), the result in D11 will NOT change.

Instead of using the WorksheetFunction.SUM, you can use VBA to apply a Sum Function to a cell using the Formula or FormulaR1C1 methods.

VBA Programming | Code Generator does work for you!

Formula Method

The formula method allows you to point specifically to a range of cells eg: D2:D10 as shown below.

vba sum formula 1

 

FormulaR1C1 Method

The FromulaR1C1 method is more flexible in that it does not restrict you to a set range of cells. The example below will give us the same answer as the one above.

vba sum formula

However, to make the formula more flexible, we could amend the code to look like this:

Wherever you are in your worksheet, the formula will then add up the 8 cells directly above it and place the answer into your ActiveCell. The Range inside the SUM function has to be referred to using the Row (R) and Column (C) syntax.

Both these methods enable you to use Dynamic Excel formulas within VBA.

There will now be a formula in D11 instead of a value.