+ Reply to Thread
Results 1 to 5 of 5

Sum & average values in range according to text in another column ?

  1. #1
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Sum & average values in range according to text in another column ?

    Hi,


    I have a drop-down list wtih in column A (text values "P","R","G" etc.), and values in column B.

    Can somebody show me how to write VBA code that will SUM all values with same text in column A, and then display average result on Userform's textbox ?

    Example:

    col A col B

    "P" 1234 result = average of those two values ?
    "P" 2334


    Thanks for any help !

    I attached sample !
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Sum & average values in range according to text in another column ?

    Here you go.

    Please note, your TextBox1 MultiLine property is set to False, you'll want to change that to True if you want each average on its own line. Otherwise you'll just see a carriage return symbol between each average.

    Please Login or Register  to view this content.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  3. #3
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Sum & average values in range according to text in another column ?

    Hi Walruseggman,

    I tried your code, but strangely It doesn't work with other ranges. This is what I need :

    Please Login or Register  to view this content.
    It doesn't work, nothing displayed on Userform. How can I fix this ?

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Sum & average values in range according to text in another column ?

    Here is your problem:

    Please Login or Register  to view this content.
    LastRow is used so that the code can find the last row used. That's why I have the syntax "A1:A" & LastRow. There's no number on that second A because that number is represented by LastRow.

    You, however, have hard coded in a last row for the range. This is perfectly fine to do, it just means you don't need to use LastRow anymore.

    So either do this, if you want to hard code in the range:

    Please Login or Register  to view this content.
    Or this, if you want the code to find the last used row (in column 5, aka column E, as you have the code now) every time the code is run:

    Please Login or Register  to view this content.
    Make sense?

  5. #5
    Forum Contributor
    Join Date
    02-09-2014
    Location
    Kamnik, Slovenia
    MS-Off Ver
    Excel 2010
    Posts
    693

    Re: Sum & average values in range according to text in another column ?

    Thanks, now it works !

    Yes, It makes sense, I made a mistake !

+ 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. Replies: 4
    Last Post: 11-19-2014, 10:13 PM
  2. [SOLVED] Count unique text values within a range based on another column
    By Sebastes in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-14-2014, 01:38 PM
  3. [SOLVED] If values in a column are equal, then need to calc average of values in other columns
    By engineerlady in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-15-2013, 02:33 PM
  4. Replies: 1
    Last Post: 01-09-2013, 01:47 PM
  5. Average range of cells in column if values in adjacent column are equal
    By RyNye in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2012, 10:12 AM
  6. Average over a text range using a vlookup In Column 2nd
    By Kenneth1024 in forum Excel General
    Replies: 6
    Last Post: 09-10-2010, 08:08 AM
  7. Re: Average over a text range using a vlookup In Column
    By Kenneth1024 in forum Excel General
    Replies: 4
    Last Post: 09-08-2010, 02:38 AM

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