+ Reply to Thread
Results 1 to 6 of 6

VBA - Finding the average of a range and ignoring zeroes - syntax question

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    VBA - Finding the average of a range and ignoring zeroes - syntax question

    The following formula accomplishes this in regular excel:

    Please Login or Register  to view this content.
    and I've tried to build it into a macro that I'm building by doing the following on a filtered range:

    Please Login or Register  to view this content.
    but it's not working.... I get the error "Run-time error 1004: Unable to get the CountIf property of the WorksheetFunction class"

    The code works fine if I'm just trying to get the sum:

    Please Login or Register  to view this content.
    So I know the problem lies in my syntax of the second half of the formula.....

    ideas?
    Last edited by lordterrin; 05-01-2014 at 01:01 PM.

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA - Finding the average of a range and ignoring zeroes - syntax question

    Hello lordterrin,

    I assume that line is nested within a With Statement. If so just try it like this.

    Please Login or Register  to view this content.
    Alternatively, you could try it this way.

    Please Login or Register  to view this content.
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: VBA - Finding the average of a range and ignoring zeroes - syntax question

    Hmmm - this did not work - since the .Countif counted ALL of the cells in the range (over 9000.) I solved this instead with the following:

    Please Login or Register  to view this content.
    Then I just used Worksheet.Function.Average as it no longer counted zeroes...

    Thanks!

  4. #4
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA - Finding the average of a range and ignoring zeroes - syntax question

    Hello lordterrin,

    You could still simplify that, by changing your formula as shown in the attached sample Workbook.

    Whether you filter the Column or not, even if there are zero's and/or blanks showing, it will return the correct average.

    Anytime better than any Loop.

    Kind Regards.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: VBA - Finding the average of a range and ignoring zeroes - syntax question

    Thanks for this Winon! I agree that the loop is... not an ideal way of doing this, but the formula doesn't take into consideration filtered ranges. In your sheet, if I filter out all but two numbers, I still get the same average number, because it's taking into consideration EVERY cell - not every cell in the filtered range. Either way - your solution will definitely help me in another part of my sheet where I need to do exactly what you've shown me, so thanks

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA - Finding the average of a range and ignoring zeroes - syntax question

    Hi lordterrin,

    I really appreciate the feedback.

    Yes, you are correct in your observation/s. What threw me here a bit, was the count only Cells >0.

    Since you are manually selecting what to filter on, you might just as well always deselect the Zero and/or Blanks as well.

    Having said that, and if you don't mind doing it, the revised attached sample Workbook will work exactly as you want.

    Regards.
    Attached Files Attached Files

+ 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] Define Average Range with Variable, Syntax Issue?
    By beaudin11 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-08-2013, 07:34 PM
  2. Ranking Values, ignoring zeroes
    By yusunghee in forum Excel General
    Replies: 1
    Last Post: 05-11-2011, 04:53 PM
  3. Replies: 1
    Last Post: 01-25-2011, 10:37 PM
  4. Replies: 6
    Last Post: 12-06-2006, 07:19 PM
  5. Average Without Zeroes
    By tom in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-03-2006, 01:40 PM

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