Range("B1").Value = WorksheetFunction.Sum(Range("A1:A10")) adds up A1:A10 and writes the total to B1 as a fixed value. To keep the total live, write a formula instead: Range("B1").Formula = "=SUM(A1:A10)".
For a list that changes length, build the range down to the last filled cell: WorksheetFunction.Sum(Range("A2", Cells(Rows.Count, "A").End(xlUp))). Separate several ranges with commas, as in WorksheetFunction.Sum(Range("A2:A10"), Range("C2:C10")), and use WorksheetFunction.SumIfs(Range("C2:C100"), Range("A2:A100"), "East") to total only the rows that meet a condition.
Store the result in a Double, not a Long or Integer. A Long rounds 10.75 to 11 and overflows above about 2.1 billion.
Numbers stored as text are skipped without a warning, so a total from imported data can come out too low; convert them with Data → Text to Columns → Finish. An error value such as #N/A in the range stops WorksheetFunction.Sum with run-time error 1004. WorksheetFunction.Aggregate(9, 6, Range("A1:A10")) sums the range and ignores errors (Excel 2010 or later).
In Excel, you can use VBA to calculate the sum of values from a range of cells or multiple ranges. In this tutorial, we are going to learn the different ways to do it.
Sum in VBA using WorksheetFunction
In VBA, there are multiple functions that you can use, but there’s no specific function for this purpose. That does not mean we can’t calculate a sum. In VBA, there’s a property called WorksheetFunction that helps you call worksheet functions in a VBA code.

Let’s sum the values from the range A1:A10.
- First, enter the WorksheetFunction property and then select the SUM function from the list.

- Next, you need to enter the starting parenthesis, as you do while entering a function in the worksheet.

- After that, we need to use the range object to refer to the range for which we want to calculate the sum.

- In the end, type the closing parenthesis and assign the function’s return value to cell B1.

Range("B1") = Application.WorksheetFunction.Sum(Range("A1:A10"))
Now when you run this code, it will calculate the sum for the values that you have in the range A1:A10 and enter the value in cell B1.

Sum Values from an Entire Column or a Row
If you want to sum an entire column or row, you just need to specify a row or column instead of the range that we have used in the earlier example. Make sure the result cell is not in the column or row that you sum. That’s why the row example enters the result in cell A3.
'for the entire column A Range("B1") = Application.WorksheetFunction.Sum(Range("A:A")) 'for entire row 1 Range("A3") = Application.WorksheetFunction.Sum(Range("1:1"))
Use VBA to Sum Values from the Selection
Now, let’s say you want to sum the values from the selected cells only. In that case, you can use a code just like the following.
Sub vba_sum_selection() Dim sRange As Range Dim iSum As Double On Error GoTo errorHandler Set sRange = Selection iSum = WorksheetFunction.Sum(Range(sRange.Address)) MsgBox iSum Exit Sub errorHandler: MsgBox "make sure to select a valid range of cells" End Sub
In the above code, we have assigned the selection to the variable “sRange” and then used that range variable’s address to get the sum. The result variable is a Double, so the sum keeps its decimals, and Exit Sub stops the code before the error message when everything works.
VBA Sum All Cells Above
The following code takes all the cells above the selected cell, sums the values from them and enters the result in the selected cell.
Sub vba_auto_sum() Dim iFirst As String Dim iLast As String Dim iRange As Range On Error GoTo errorHandler iFirst = Selection.End(xlUp).End(xlUp).Address iLast = Selection.End(xlUp).Address Set iRange = Range(iFirst & ":" & iLast) ActiveCell = WorksheetFunction.Sum(iRange) Exit Sub errorHandler: MsgBox "make sure to select a valid range of cells" End Sub
Sum a Dynamic Range using VBA
In the same way, you can use a dynamic range while using VBA to sum values. The code below sums a 5 x 5 range that starts one row below and one column to the right of the selected cell, so the range moves with your selection.
Sub vba_dynamic_range_sum() Dim iFirst As String Dim iLast As String Dim iRange As Range On Error GoTo errorHandler iFirst = Selection.Offset(1, 1).Address iLast = Selection.Offset(5, 5).Address Set iRange = Range(iFirst & ":" & iLast) ActiveCell = WorksheetFunction.Sum(iRange) Exit Sub errorHandler: MsgBox "make sure to select a valid range of cells" End Sub
Sum a Dynamic Column or a Row
In the same way, if you want to use a dynamic column, you can use the following code, which takes the column of the active cell and sums all the values that you have in it.
Sub vba_dynamic_column() Dim iCol As Long On Error GoTo errorHandler iCol = ActiveCell.Column MsgBox WorksheetFunction.Sum(Columns(iCol)) Exit Sub errorHandler: MsgBox "make sure to select a valid range of cells" End Sub
And here’s the code for a row, which uses the row of the active cell.
Sub vba_dynamic_row() Dim iRow As Long On Error GoTo errorHandler iRow = ActiveCell.Row MsgBox WorksheetFunction.Sum(Rows(iRow)) Exit Sub errorHandler: MsgBox "make sure to select a valid range of cells" End Sub
Using SUMIF with VBA
Just like SUM, you can use the SUMIF function to sum values with criteria, like in the following example.

Sub vba_sumif() Dim cRange As Range Dim sRange As Range Set cRange = Range("A2:A13") Set sRange = Range("B2:B13") Range("C2") = _ WorksheetFunction.SumIf(cRange, "Product B", sRange) End Sub
In the above code, cRange is the range with the criteria (product names), “Product B” is the criteria, and sRange is the range with the values to sum. The code sums the values for “Product B” and enters the result in cell C2.
Using SUMIFS with VBA for More Than One Condition
For two or more conditions, use SumIfs. Here, the range to sum comes first, and then the pairs of range and criteria. The code below sums the values in column B where column A has “Product B” and column C has “North”:
Sub vba_sumifs()
Range("E2") = WorksheetFunction.SumIfs(Range("B2:B13"), Range("A2:A13"), "Product B", Range("C2:C13"), "North")
End Sub
Sum Values from Multiple Ranges
Just like the SUM function in a worksheet, you can give more than one range to WorksheetFunction.Sum, separated by commas. The code below adds all the values from A1:A10 and C1:C10:
Sub vba_sum_multiple_ranges()
Range("E1") = Application.WorksheetFunction.Sum(Range("A1:A10"), Range("C1:C10"))
End Sub
Sum Only the Visible Cells After a Filter
Sum adds all the cells in the range, including the rows hidden by a filter. To sum only the visible cells, use Subtotal with 109 as the first argument:
Sub vba_sum_visible()
Range("B1") = Application.WorksheetFunction.Subtotal(109, Range("A2:A100"))
End Sub
With 109, Subtotal skips rows that are filtered out and rows that you have hidden manually.
Enter the SUM Formula with VBA
All the codes above put a fixed value in the cell, so the total doesn’t change when the values change. If you want the total to update on its own, use the code to enter the SUM formula in the cell:
Sub vba_sum_formula()
Range("B1").Formula = "=SUM(A1:A10)"
End Sub
Common Problems with SUM in VBA
- Decimals are lost: If you store the result in a Long or Integer variable, VBA rounds it. Use Double for the result.
- Numbers stored as text: Sum skips them, so the total can be lower than you expect. Convert them to numbers first.
- Error values in the range: If any cell has an error like #N/A, WorksheetFunction.Sum stops the code with a run-time error. Fix the errors in the data first.
- Result cell inside the range: If the cell that gets the result is inside the range that you sum, the old value in that cell is added to the new total. Keep the result cell outside the range.
Frequently Asked Questions
How do I sum values on another sheet in VBA?
Put the sheet name in front of the range, like WorksheetFunction.Sum(Worksheets(“Sheet2”).Range(“A1:A10”)). You don’t need to activate that sheet first.
What does Sum return if there are no numbers?
It returns 0. Unlike Average, Sum doesn’t give an error for an empty range or a range with only text.
Can I sum values with a loop instead?
Yes. You can go through each cell with For Each and add the numbers to a variable, like in the code below. It’s slower than WorksheetFunction.Sum, but useful when you need to check each cell before you add it.
Sub vba_sum_loop()
Dim c As Range
Dim total As Double
For Each c In Range("A1:A10")
If IsNumeric(c.Value) And Not IsEmpty(c.Value) Then total = total + c.Value
Next c
MsgBox total
End Sub
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.