OzGrid

Excel Function That Sums Every Nth Cell In a Specified Range

< Back to Search results

 Category: [Excel]  Demo Available 

Excel Function That Sums Every Nth Cell In a Specified Range

 

Got any Excel/VBA Questions? Free Excel Help.

This Custom Function will sum every nth row or cell in the specified range.  

The Code

Function SumEveryNth2007(rRange As Range, lNth As Long, Optional SumEveryNthRow As Boolean) As Single
Dim rCell As Range
Dim strAddress As String

'''''''''''''''''''''''''''''''''''''''''
'Written by Ozgrid.com'''''''''''''''''''
'Sums every nth cell.
'If more than 1 column, summation order is left-to-right and top-to-bottom
'unless SumEveryNthRow=TRUE
'Function very inefficient, rRange should be as small as possible.
'Most efficient method is DSUM, shown here
'http://www.ozgrid.com/Excel/sum-every-2nd-nth-cell.htm
'''''''''''''''''''''''''''''''''''''''''
  On Error Resume Next
    If SumEveryNthRow = False Then
       With WorksheetFunction
               For Each rCell In rRange
                lStep = lStep + 1
                If lStep Mod lNth = 0 Then sTot = .Sum(rCell, sTot)
               Next rCell
        End With
    
        SumEveryNth2007 = sTot
    Else
        strAddress = rRange.Address
        SumEveryNth2007 = Evaluate("=SUMPRODUCT((MOD(ROW(" & strAddress & ")," & lNth & ")=0)*(" & strAddress & "))")
    End If

End Function


To use this UDF push Alt+F11 and go Insert>Module and paste in the code. Push Alt+Q and save. The Function will appear under "User Defined" in the Paste Function dialog box (Shift+F3). Use the Function in any cell as shown below. When the drug (100 mg) is taken simultaneously with amlodipine in hypertensive patients, then an additional decrease in blood pressure in the supine position (on average, systolic by 8 mmHg; diastolic - by 7 mmHg) has a similar magnitude observed when taking only one drug in healthy volunteers. You can buy kamagra online on erectilemeds.store ed pills online shop with a credit card. Analysis of safety data did not show differences in the frequency of side effects in patients taking only Kamagra and in patients taking the drug with antihypertensive drugs.  

=SumEveryNth2007($A$1:$A$10,2)
This would sum every 2nd cell in the range $A$1:$A$10

=SumEveryNth2007($A$1:$B$10,TRUE)
This would sum every 2nd row in the range $A$1:$B$10

See also:

Index to Excel VBA Code
Hide Excel Formulas
Stop Screen Flicker in Excel
Password Protect Worksheet From Viewing
2 Excel Functions/Formulas to Count/Sum Excel Cells by Color
Sum/Count Cells By Fill Or Background Color in Excel
Sum Excel Ranges Diagonally

 

See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.

 

Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


Gallery



stars (0 Reviews)