![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: May 2002
Location: California
Posts: 4
|
Is there a way to sum/average ever other cell or every 3rd or 4th cell and so on...?
|
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
|
you need the udf.........
Function sumodd(look As Range) As Double Dim c As Range Dim dbltotal As Double Dim intcount As Integer For Each c In look If c.Row Mod 2 Then ' for even rows i.e 2nd row, 4th row ' if c.row mod 2 = 0 then dbltotal = c + dbltotal intcount = 1 + intcount End If Next c ' you need the average 'sumodd = dbltotal / intcount sumodd = dbltotal End Function nishith desai http://www.pexcel.com |
|
|
|
|
|
#3 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
Actually, you can do this directly in Excel... take a look at the Tip of the Day for...
January 24th, 2002. The archive got messed up, so here it is: Quote:
|
|
|
|
|
|
|
#4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
MrExcel MVP
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
|
There is always some uncertainty involved in expressions like "sum/average ever other cell or every 3rd or 4th cell." The question arises as to whether to start summing/averaging from the first cell of the target range or at Nth other cell.
I believe a well-parameterized UDF is the way to go. A flexible UDF should accept a Range that can be either vertical (a range in a column) or horizontal (a range in a row), a Start cell (either from the first cell on or from Nth cell on), a Function parameter (at least for SUM and AVERAGE), and N which indicates (every Nth) cell to be included in Function's domain. The sheet figure shows a bit more flexible formula approach for vertical ranges.
You can see the formula of cells only click each above hyperlinks The above image was automatically written by HtmlMakerVer1.12 If you want this code, click here and Colo will email the file to you. D1 must be either 1 or equal to D2. D2 records N of the every Nth cell. D3 has a formula for summing: =SUMPRODUCT((MOD(ROW(Range1)-ROW(OFFSET(Range1,0,0,1,1))+($D$1=$D$2),$D$2)=0)*(Range1)) D4 likewise for the second vertical range. D8 has a formula for averaging: =SUMPRODUCT((MOD(ROW(Range1)-ROW(OFFSET(Range1,0,0,1,1))+($D$6=$D$7),$D$7)=0)*(Range1))/MAX(1,SUMPRODUCT((MOD(ROW(Range1)-ROW(OFFSET(Range1,0,0,1,1))+($D$6=$D$7),$D$7)=0)+0)) D9 likewise for the second vertical range. Aladin |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
#5 |
|
Board Regular
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
|
Function sumodd(look As Range, t As Byte) As Double
Dim c As Range Dim dbltotal As Double Dim intcount As Integer Dim intcountrange As Integer Dim blnrc As Boolean If t > 1 Then Exit Function If t = 1 Then blnrc = True For Each c In look If blnrc Then If c.Column Mod 2 Then dbltotal = c + dbltotal intcount = 1 + intcount End If ElseIf t = 0 Then If c.Row Mod 2 Then ' for even rows i.e 2nd row, 4th row ' if c.row mod 2 = 0 then dbltotal = c + dbltotal intcount = 1 + intcount End If End If Next c ' you need the average 'sumodd = dbltotal / intcount sumodd = dbltotal End Function i think this can work.. and can solve the problem which aladin had mention in his answere. Thanks aladin,, for making the point. i request suggestion for above code. nisht http://www.pexcel.com |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|