+ Reply to Thread
Results 1 to 2 of 2

Excel VBA code to Average Non Zeros for Multiple Ranges

Hybrid View

theclockmaker Excel VBA code to Average Non... 03-11-2020, 01:00 PM
BMV Re: Excel VBA code to Average... 03-11-2020, 01:37 PM
  1. #1
    Registered User
    Join Date
    06-29-2011
    Location
    Fort Lauderdale
    MS-Off Ver
    Excel 2018
    Posts
    24

    Excel VBA code to Average Non Zeros for Multiple Ranges

    I created this VBA code to average one or more ranges of nonzero numbers.

    Some example uses would be:

    =avgNonZeros(B2:B10,C2:C10,E2:E10)
    =avgNonZeros(Q22,R22,U22:Z22)
    =avgNonZeros(B2:B10,C10,E10)
    =avgNonZeros(B2:B10)

    Option Explicit
    Function avgNonZeros(ParamArray rangeList() As Variant) As Variant
    ** 'Returns the average for all nonzeros of rangeList.
    ** 'rangeList may be one or multiple ranges.
    ** Dim cell As Range
    ** Dim i As Long
    ** Dim totSum As Long
    ** Dim cnt As Long
    ** DoEvents 'allows calculations prior to performing
    ** avgNonZeros = 0 'default return
    ** For i = LBound(rangeList) To UBound(rangeList)
    ***** For Each cell In rangeList(i)
    ******** If cell <> 0 Then
    *********** totSum = totSum + cell
    *********** cnt = cnt + 1
    ******** End If
    ***** Next cell
    ** Next i
    ** If cnt <> 0 Then avgNonZeros = totSum / cnt
    End Function

  2. #2
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: Excel VBA code to Average Non Zeros for Multiple Ranges

    Function avgNonZeros(ParamArray rangeList() As Variant) As Double
    'Returns the average for all nonzeros of rangeList.
    'rangeList may be one or multiple ranges.
    Dim Area As Variant
    Dim totSum As Long
    Dim cnt As Long
    DoEvents 'allows calculations prior to performing
    For Each Area In rangeList
        cnt = cnt + WorksheetFunction.Count(Area) - WorksheetFunction.CountIf(Area, "=0")
        totSum = totSum + WorksheetFunction.Sum(Area)
    Next
    If cnt <> 0 Then avgNonZeros = totSum / cnt
    End Function
    Last edited by BMV; 03-11-2020 at 01:45 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Averageif, not counting zeros, but in multiple ranges)
    By Alphabex in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 01-08-2022, 01:38 PM
  2. How do I average multiple columns (ignoring zeros and blank cells)?
    By yeto in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-04-2020, 12:36 PM
  3. AVERAGE - Multiple VLOOKUPs whilst ignoring zeros or blanks
    By Jake_1 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-20-2020, 01:00 PM
  4. [SOLVED] Averaging across multiple ranges, not counting zeros, and the [h]:mm format
    By Alphabex in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-28-2016, 03:43 PM
  5. [SOLVED] how to average multiple averages that can contain zeros
    By BJB_88 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-16-2014, 07:06 PM
  6. [SOLVED] How do I get the average across multiple cells not in a range and not count the zeros/text
    By sponge_designs in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-31-2013, 03:05 AM
  7. [SOLVED] Average for non-consecutive cells excluding zeros (even when they all contain zeros)
    By pao13 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-05-2012, 04:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1