+ Reply to Thread
Results 1 to 7 of 7

Help with an average based on criteria using VBA

  1. #1
    Registered User
    Join Date
    07-17-2014
    Location
    United States
    MS-Off Ver
    MS Office Professional 2010
    Posts
    3

    Help with an average based on criteria using VBA

    I would like to use VBA to calculate an average of the values in column C, for each Criterion in column A (similar to averageif). Ideally this code would scan column A for a unique string, when it finds one it would take the average of the corresponding values in the C column. Lastly, it would take the original string from column A along with the calculated average and form two new columns that uses one row for each criteria in Column A.

    This example may explain things better:

    INPUT
    A------------B-------- C
    A5006-- 5/10/2014-- 226
    A5006-- 5/17/2014-- 198
    A5006-- 5/24/2014-- 215
    APD 212-- 5/10/2014-- 33,682
    APD 212-- 5/17/2014-- 33,063
    5055-- 5/10/2014-- 387
    5055-- 5/17/2014-- 371
    5055-- 5/24/2014-- 405
    5055-- 5/31/2014-- 383

    OUTPUT
    D ---- E
    A5006 -- 213.5
    APD 212 -- 33372.5
    5055 -- 386.5
    Last edited by WhoaIneedAhand; 07-17-2014 at 05:49 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Help with an average based on criteria using VBA

    Why not use a Pivot Table:


    A
    B
    C
    D
    E
    F
    1
    A
    B
    C*
    2
    A5006
    05/10/2014
    226
    3
    A5006
    5/17/2014
    198
    4
    A5006
    5/24/2014
    215
    5
    APD 212
    05/10/2014
    33,682
    6
    APD 212
    5/17/2014
    33,063
    7
    5055
    05/10/2014
    387
    8
    5055
    5/17/2014
    371
    9
    5055
    5/24/2014
    405
    10
    5055
    5/31/2014
    383
    11
    12
    OUTPUT
    13
    D
    E
    Row Labels
    Average of C*
    14
    A5006
    213.5
    5055
    386.50
    15
    APD 212
    33372.5
    A5006
    213.00
    16
    5055
    386.5
    APD 212
    33372.50
    17



    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Help with an average based on criteria using VBA

    You can record a macro while you create the Pivot Table. This is the unedited code:

    Please Login or Register  to view this content.

    It can, of course, be tweaked, refined and generalised ... but it's a start.

    Regards, TMS

  4. #4
    Registered User
    Join Date
    07-17-2014
    Location
    United States
    MS-Off Ver
    MS Office Professional 2010
    Posts
    3

    Re: Help with an average based on criteria using VBA

    TMS,

    The pivot table method serves the function of what I would like it to do, however, I will be reporting that information back to a sheet that will perform many more calculations using the value from column A and the average. I am an intern trying to learn VBA while designing a template that will be reused. From what I have seen the pivot table method seems likely to produce errors if the naming is not proper in the macro.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Help with an average based on criteria using VBA

    Thanks for the rep

    You're absolutely right. As a result, it is necessary to clear the ground before you create your Pivot Table.

    The examples below create two Pivot Tables. The first creates a PT from the source data, the second creates a second PT using the cache.

    Both delete the existing PT before recreating it.

    An alternative approach, I guess, if you are learning VBA, is to record a macro whilst you use an Advanced Filter to extract unique entries from column A and then add a formula AVERAGEIF. I'll leave that option with you


    Please Login or Register  to view this content.

    See the attached workbook.


    Regards, TMS
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    07-17-2014
    Location
    United States
    MS-Off Ver
    MS Office Professional 2010
    Posts
    3

    Re: Help with an average based on criteria using VBA

    Here is a workbook with the Macro that I have created so far. It uses a lot of select statements to get the job done. Any advice or revisions are appreciated.
    FIXME.xlsm

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,464

    Re: Help with an average based on criteria using VBA

    I suggest you start a new thread and link back to this one. That way, you'll get more input from the community. A thread with six or seven posts and a Forum guru involved is unlikely to generate any interest.

    Regards, TMS

+ 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] Need to find an average based on several criteria.
    By Crow47 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-22-2013, 06:14 PM
  2. [SOLVED] Average, max and min based on 3 criteria
    By VBADUD in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-06-2013, 11:07 PM
  3. [SOLVED] Average based on a criteria
    By caabdul in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-30-2012, 12:14 AM
  4. GET AVERAGE based on multiple criteria
    By pphg in forum Excel General
    Replies: 1
    Last Post: 11-16-2012, 05:47 AM
  5. [SOLVED] Calculating an average based on 2 and 3 criteria
    By craggergirl in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-24-2006, 11:10 AM

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