Home

Formula for adding non-consecutive cells across entire row

%3CLINGO-SUB%20id%3D%22lingo-sub-751708%22%20slang%3D%22en-US%22%3EFormula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751708%22%20slang%3D%22en-US%22%3E%3CP%3EHoping%20someone%20can%20help%3B%20I'm%20trying%20to%20automatically%20add%20non-consecutive%20cell%20values%20together%20and%20am%20currently%20using%20the%20old%20fashioned%20approach%20of%20SUM(cell1%2C%20cell7%2C%20cell13...).%20The%20pattern%20remains%20the%20same%20(i.e.%20wanting%20to%20add%20values%206%20cells%20apart)%20but%20as%20I%20add%20more%20data%2C%20I%20would%20like%20to%20automatically%20add%20the%20new%20data%20to%20the%20SUM%20total.%20There%20must%20be%20a%20way%20of%20creating%20a%20pattern%20whereby%20Excel%20only%20SUMs%20every%206th%20cell%20but%20my%20formula%20work%20is%20pretty%20basic...%20Note%20that%20I%20will%20want%20the%20formula%20to%20work%20for%20the%20same%20pattern%20(i.e.%20every%206th%20cell)%20but%20starting%20at%20cell%202%20instead%20of%20cell%201%20so%20I%20would%20need%20to%20define%20a%20start%20and%20end%20cell%20for%20the%20pattern%20to%20commence%20and%20end.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHopefully%20you%20can%20see%20what%20I'm%20getting%20at!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-751708%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751798%22%20slang%3D%22en-US%22%3ERe%3A%20Formula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751798%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F375257%22%20target%3D%22_blank%22%3E%40nevans1982%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20to%20sum%20every%206th%20cell%20in%20first%20row%2C%20it%20could%20be%3C%2FP%3E%0A%3CPRE%3E%3DSUMPRODUCT(IF(MOD(COLUMN(INDEX(1%3A1%2C0%2CFirstColumn)%3AINDEX(1%3A1%2C0%2CLastColumn))-FirstColumn%2CPeriod)%3D0%2CINDEX(1%3A1%2C0%2CFirstColumn)%3AINDEX(1%3A1%2C0%2CLastColumn)%2C0))%3C%2FPRE%3E%0A%3CP%3Ewhere%20FirstColumn%2C%20LastColumn%20and%20Period%20are%20named%20cells%20with%20parameters.%20Similar%20if%20every%206th%20cell%20in%20the%20column.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-752387%22%20slang%3D%22en-US%22%3ERe%3A%20Formula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-752387%22%20slang%3D%22en-US%22%3ETo%20add%20the%20values%20in%20Column%20A%2C%20but%20which%20includes%20only%20A2%2C%20A8%2C%20A14%2C%20A20%2C%20and%20so%20forth%2C%20try%20this%20formula%3A%3CBR%20%2F%3E%3DSUMPRODUCT((MOD(ROW(Range)%2B%3CBR%20%2F%3E4%2C6)%3D0)*%3CBR%20%2F%3ERange)%3CBR%20%2F%3ENote%20that%20Range%20is%20a%20defined%20name%20that%20refers%20to%20the%20values%20in%20Column%20A%2C%20starting%20in%20A2.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-752419%22%20slang%3D%22en-US%22%3ERe%3A%20Formula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-752419%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F280482%22%20target%3D%22_blank%22%3E%40Twifoo%3C%2FA%3E%26nbsp%3B%2C%20that%20works%20only%20if%20there%20are%20no%20texts%20in%20between.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-753787%22%20slang%3D%22en-US%22%3ERe%3A%20Formula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-753787%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F375257%22%20target%3D%22_blank%22%3E%40nevans1982%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20willing%20to%20use%20a%20VBA%20code%20solution%2C%20here%20is%20a%20UDF%20(user%20defined%20function)%20that%20you%20can%20use...%3C%2FP%3E%0A%3CPRE%3EFunction%20SumAcross(StartCell%20As%20Range%2C%20SkipAmount%20As%20Long)%20As%20Double%0A%20%20Dim%20C%20As%20Long%2C%20LastCol%20As%20Long%0A%20%20Application.Volatile%0A%20%20LastCol%20%3D%20Cells(StartCell.Row%2C%20Columns.Count).End(xlToLeft).Column%0A%20%20For%20C%20%3D%20StartCell.Column%20To%20LastCol%20Step%20SkipAmount%0A%20%20%20%20SumAcross%20%3D%20SumAcross%20%2B%20Cells(StartCell.Row%2C%20C).Value%0A%20%20Next%0AEnd%20Function%3C%2FPRE%3E%0A%3CP%3EThe%20first%20argument%20to%20the%20function%20is%20a%20reference%20to%20the%20starting%20cell%20and%20the%20second%20argument%20is%20the%20amount%20of%20cells%20to%20skip%20over%20while%20traveling%20from%20the%20starting%20cell%20across%20that%20starting%20cell's%20row.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHOW%20TO%20INSTALL%20UDFs%3CBR%20%2F%3E------------------------------------%3CBR%20%2F%3EIf%20you%20are%20new%20to%20UDFs%2C%20they%20are%20easy%20to%20install%20and%20use.%20To%20install%20it%2C%20simply%20press%20ALT%2BF11%20to%20go%20into%20the%20VB%20editor%20and%2C%20once%20there%2C%20click%20Insert%2FModule%20on%20its%20menu%20bar%2C%20then%20copy%2Fpaste%20the%20above%20code%20into%20the%20code%20window%20that%20just%20opened%20up.%20That's%20it....%20you%20are%20done.%20You%20can%20now%20use%20SumAcross%20just%20like%20it%20was%20a%20built-in%20Excel%20function.%20For%20example%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DSumAcross(A2%2C6)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20using%20XL2007%20or%20above%2C%20make%20sure%20you%20save%20your%20file%20as%20an%20%22Excel%20Macro-Enabled%20Workbook%20(*.xlsm)%20and%20answer%20the%20%22do%20you%20want%20to%20enable%20macros%22%20question%20as%20%22yes%22%20or%20%22OK%22%20(depending%20on%20the%20button%20label%20for%20your%20version%20of%20Excel)%20the%20next%20time%20you%20open%20your%20workbook.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-754194%22%20slang%3D%22en-US%22%3ERe%3A%20Formula%20for%20adding%20non-consecutive%20cells%20across%20entire%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-754194%22%20slang%3D%22en-US%22%3E%3CP%3ELet's%20expand%20the%20function%20I%20posted%20above%20to%20add%20an%20optional%20third%20argument...%20the%20stop%20cell.%20If%20omitted%2C%20the%20last%20column%20will%20be%20calculated%20by%20the%20code%20and%20will%20be%20the%20last%20column%20with%20data%20on%20the%20start%20cell's%20row%3B%20but%20if%20a%20cell%20reference%20is%20provided%2C%20then%20its%20column%20will%20be%20used%20as%20the%20last%20column%20to%20calculate%20up%20to.%20Here%20is%20the%20code...%3C%2FP%3E%0A%3CPRE%3EFunction%20SumAcross(StartCell%20As%20Range%2C%20SkipAmount%20As%20Long%2C%20Optional%20StopCell%20As%20Variant)%20As%20Double%0A%20%20Dim%20C%20As%20Long%2C%20LastCol%20As%20Long%0A%20%20Application.Volatile%0A%20%20If%20IsMissing(StopCell)%20Then%0A%20%20%20%20LastCol%20%3D%20Cells(StartCell.Row%2C%20Columns.Count).End(xlToLeft).Column%0A%20%20Else%0A%20%20%20%20LastCol%20%3D%20StopCell.Column%0A%20%20End%20If%0A%20%20For%20C%20%3D%20StartCell.Column%20To%20LastCol%20Step%20SkipAmount%0A%20%20%20%20SumAcross%20%3D%20SumAcross%20%2B%20Cells(StartCell.Row%2C%20C).Value%0A%20%20Next%0AEnd%20Function%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
nevans1982
Occasional Visitor

Hoping someone can help; I'm trying to automatically add non-consecutive cell values together and am currently using the old fashioned approach of SUM(cell1, cell7, cell13...). The pattern remains the same (i.e. wanting to add values 6 cells apart) but as I add more data, I would like to automatically add the new data to the SUM total. There must be a way of creating a pattern whereby Excel only SUMs every 6th cell but my formula work is pretty basic... Note that I will want the formula to work for the same pattern (i.e. every 6th cell) but starting at cell 2 instead of cell 1 so I would need to define a start and end cell for the pattern to commence and end.

 

Hopefully you can see what I'm getting at!

5 Replies

@nevans1982 

If to sum every 6th cell in first row, it could be

=SUMPRODUCT(IF(MOD(COLUMN(INDEX(1:1,0,FirstColumn):INDEX(1:1,0,LastColumn))-FirstColumn,Period)=0,INDEX(1:1,0,FirstColumn):INDEX(1:1,0,LastColumn),0))

where FirstColumn, LastColumn and Period are named cells with parameters. Similar if every 6th cell in the column.

 

To add the values in Column A, but which includes only A2, A8, A14, A20, and so forth, try this formula:
=SUMPRODUCT((MOD(ROW(Range)+
4,6)=0)*
Range)
Note that Range is a defined name that refers to the values in Column A, starting in A2.

@Twifoo , that works only if there are no texts in between.

@nevans1982

 

If you are willing to use a VBA code solution, here is a UDF (user defined function) that you can use...

Function SumAcross(StartCell As Range, SkipAmount As Long) As Double
  Dim C As Long, LastCol As Long
  Application.Volatile
  LastCol = Cells(StartCell.Row, Columns.Count).End(xlToLeft).Column
  For C = StartCell.Column To LastCol Step SkipAmount
    SumAcross = SumAcross + Cells(StartCell.Row, C).Value
  Next
End Function

The first argument to the function is a reference to the starting cell and the second argument is the amount of cells to skip over while traveling from the starting cell across that starting cell's row.

 

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use SumAcross just like it was a built-in Excel function. For example,

 

=SumAcross(A2,6)

 

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Let's expand the function I posted above to add an optional third argument... the stop cell. If omitted, the last column will be calculated by the code and will be the last column with data on the start cell's row; but if a cell reference is provided, then its column will be used as the last column to calculate up to. Here is the code...

Function SumAcross(StartCell As Range, SkipAmount As Long, Optional StopCell As Variant) As Double
  Dim C As Long, LastCol As Long
  Application.Volatile
  If IsMissing(StopCell) Then
    LastCol = Cells(StartCell.Row, Columns.Count).End(xlToLeft).Column
  Else
    LastCol = StopCell.Column
  End If
  For C = StartCell.Column To LastCol Step SkipAmount
    SumAcross = SumAcross + Cells(StartCell.Row, C).Value
  Next
End Function
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies