 Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using SUM In a Macro.

# Using SUM In a Macro by Allen Wyatt
(last updated October 25, 2016)

Bob has a need to use the SUM function in a macro in order to find the sum of all the values in a column. The problem is that the number of cells to be summed will vary; for one run of the macro it could be 100 cells, while on the next it could be 300 and on the third only 25.

First, it is easy to use most worksheet functions (such as SUM) from within a macro. All you need to do is to preface the function name with "Application.WorksheetFunction." or simply "WorksheetFunction." Thus, if you know that each run of the macro will require summing A1:A100, then A1:A300, and finally A1:A25, you could use a macro like this:

```Public Sub Sum_Demo()
Dim myRange
Dim Results
Dim Run As Long

For Run = 1 To 3
Select Case Run
Case 1
myRange = Worksheets("Sheet1").Range("A1", "A100")
Case 2
myRange = Worksheets("Sheet1").Range("A1", "A300")
Case 3
myRange = Worksheets("Sheet1").Range("A1", "A25")
End Select
Results = WorksheetFunction.Sum(myRange)
Range("B" & Run) = Results
Next Run
End Sub
```

This macro uses a For . . . Next loop to specify different ranges of cells to be summed. It then uses the SUM worksheet function to assign the sum to the Results variable, which is (finally) stuffed into a cell in column B. The results of the first run are put in B1, the second in B2, and the third in B3.

While this particular macro may not be that useful, it shows several helpful techniques, such as how to define a named range, how to use the SUM function, and how to stuff the sum into a cell. What the macro doesn't do is to show how to select a variable number of cells to be summed. To do this, it is best to rely upon the End method of the Range object. The following code line shows how you can stuff the sum of the range starting at A1 and extending to just before the first blank cell in the column:

```myRange = ActiveSheet.Range("A1", Range("A1").End(xlDown))
Range("B1") = WorksheetFunction.Sum(myRange)
```

Note that a range (myRange) is defined as beginning with A1 and extending through whatever the End method returns. This is then summed and stuffed into B1.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9180) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Using SUM In a Macro.

##### Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

##### MORE FROM ALLEN

No-border Text Boxes by Default

Text boxes can be a great design element when laying out your documents. If you want those text boxes to have no borders ...

Discover More

Modifying What is Started when You Start Windows

Did you know that Windows automatically starts extra programs whenever you boot your system? If you want to see which ...

Discover More

Creating a Named Range

Named ranges can be a great boon in creating easily understandable formulas. Here's what they are an how to define them.

Discover More Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

##### More ExcelTips (ribbon)

Running a Procedure when a Workbook is Opened

Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.

Discover More

Disabling All Function Keys Except One

Disabling function keys is rather easy to do when you rely on the OnKey method in a macro. This tip looks at how you can ...

Discover More

Summing Only Visible Values

When you use SUM to determine the total of a range of values, Excel doesn't really pay attention to whether the values ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2019-08-27 09:36:30

Zsolt

Can someone help me please as I just can't figure out why the above mentioned SUM method gives me the result of Range("A4", Range("H4").End(xlDown) instead of the below declared (at the very end) ActiveSheet.Range("H4", Range("H4").End(xlDown))

By the way I know that this code probably not so efficient but please note that I am only a beginner with VBA : )

Thank you very much in advance for any suggestions and I wish you a further nice day.

Dim FindString As String
Dim Rng As Range
Dim lprng As Range, cell As Range
Dim help As String
Dim rnghelp

Set lprng = Range("J3:J5")

FindString = "Totals : "

For Each cell In lprng

help = cell.Value

With Sheets(help).Range("A:S")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With

Range(Cells(Selection.Row, 1), Cells(Selection.Row, 19)).Insert Shift:=xlDown

Sheets("Sheet3").Select

Range(Cells(cell.Row, 11), Cells(cell.Row, 29)).Copy

Sheets(help).Select

Range(Cells(Selection.Row, 1), Cells(Selection.Row, 19)).PasteSpecial xlPasteValues

With Sheets(help).Range("A:S")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With

rnghelp = ActiveSheet.Range("H4", Range("H4").End(xlDown))

ActiveCell.Offset(, 1) = WorksheetFunction.Sum(rnghelp)

Next cell

End Sub

2019-08-27 04:55:17

Zsolt

Can someone help me please as I just can't figure out why the above mentioned SUM method gives me the result of Range("A4", Range("H4").End(xlDown) instead of the below declared (at the very end) ActiveSheet.Range("H4", Range("H4").End(xlDown))

By the way I know that this code probably not so efficient but please note that I am only a beginner with VBA : )

Thank you very much in advance for any suggestions and I wish you a further nice day.

Dim FindString As String
Dim Rng As Range
Dim lprng As Range, cell As Range
Dim help As String
Dim rnghelp

Set lprng = Range("J3:J5")

FindString = "Totals : "

For Each cell In lprng

help = cell.Value

With Sheets(help).Range("A:S")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With

Range(Cells(Selection.Row, 1), Cells(Selection.Row, 19)).Insert Shift:=xlDown

Sheets("Sheet3").Select

Range(Cells(cell.Row, 11), Cells(cell.Row, 29)).Copy

Sheets(help).Select

Range(Cells(Selection.Row, 1), Cells(Selection.Row, 19)).PasteSpecial xlPasteValues

With Sheets(help).Range("A:S")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With

rnghelp = ActiveSheet.Range("H4", Range("H4").End(xlDown))

ActiveCell.Offset(, 1) = WorksheetFunction.Sum(rnghelp)

Next cell

End Sub

2016-10-25 12:23:19

Thomas Papavasiliou

If Bob wants to use the Sum function for all the values of column “A” regardless of their number, a very simple method is to use an empty cell (in the example, cell D1) and use the code:

Cells(1, 4) = Application.WorksheetFunction.Sum(Columns(1))

2014-04-14 09:00:55

Jeff

FYI, Jim's first method assumes there are no blank cells within the range of cells to be summed. See the Range.End property.

2014-04-14 07:38:58

Jim

Linda,

Here is a formula that one would use to NOT make an absolute reference. Notice the "-10" would ONLY count 10 rows above where the "R7C" makes E7 the top of the column no matter how many rows are involved.

ActiveCell.FormulaR1C1 = "=SUM(R[-10]C:R[-2]C)"

Hope this helps.

2014-04-13 11:04:14

Linda W

Jim - How does the \$ get in the formula?

2014-04-13 10:18:01

Jim Sweet

Here's how I do it. To sum values dynamically from cell E6 to bottom of column no matter how many rows there are (using relative references)

Sheet1.Select
Range("E6").Select
Selection.End(xlDown).Select
ActiveCell.Offset(2, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "=SUM(R7C:R[-1]C)"

The resultant formula is: =SUM(E\$7:E23)

Notice that "E\$7" is to make absolute reference for first number in column.

##### This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.