+ Reply to Thread
Results 1 to 13 of 13

Help with formula for unique values - VBA

  1. #1
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Help with formula for unique values - VBA

    Hello,

    I'm having trouble coming up with a formula that would count unique values in a particular column.
    I have, (In column C,) a bunch of values that look similar to this... 050316-22614-03, and I want a tally of unique values.

    I can't use a range, ie: C2:C28, because this column is populated from another sheet via a macro and the # of rows will always be different. It needs to count unique values in the whole column, ie. C:C

    This is part of a VBA code.

    Anyone help?

    Cheers,
    Richard
    Last edited by rkohman; 08-19-2016 at 11:27 AM. Reason: Forgot to mention critical detail

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,055

    Re: Help with formula for unique values

    here's one way, using an array formula:
    =SUM(IF(LEN(A:A)>0,1/COUNTIF(A:A,A:A)))

    Array Formulae are a little different from ordinary formulas in that they MUST be confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER.

    You will know the array is active when you see curly braces { } appear around the outside of your formula. If you do not CTRL+SHIFT+ENTER you will (almost always) get an error message or an incorrect answer. Press F2 on that cell and try again.

    Don't type the curly braces yourself - it won't work...
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Re: Help with formula for unique values

    Thanks Glenn,

    Is there another way?... this is being entered as part of a 30 page VB code.... so I don't know if that particular method will work...

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,055

    Re: Help with formula for unique values

    What don't you like about it??

    here's another array formula:

    =SUM(IF(1-ISERROR(A:A),IF(A:A<>"",1))/COUNTIF(A:A,A:A&""))

  5. #5
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Re: Help with formula for unique values

    Sorry... It wasn't that I didn't like it.... I just wasn't sure how it was going to work if you had to press CTRL+SHIFT+ENTER if the formula was being entered into the cell by a macro as part of a larger script.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,916

    Re: Help with formula for unique values

    Are you saying that this is to be incorporated into your VBA? If so, then I'm not sure how anyone helping could have known this at the outset, since you never mentioned it in your OP and this isn't the VBA section of the forum. It might be worth editing your original thread title to reflect this.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Re: Help with formula for unique values

    Sorry, that was my mistake. I will edit my original post.

  8. #8
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,055

    Re: Help with formula for unique values - VBA

    =SUMPRODUCT(1/COUNTIF(A:A,A:A&""))

    last attempt!!

  9. #9
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Re: Help with formula for unique values - VBA

    Thanks Glenn, That seems to be much closer. I've added a -1 to the end to remove the column header... but it is still giving me 1 more than is in the column.

    I have 18 unique values, (not including the column header,) and with the -1 it is showing 19.

    Any ideas why?

  10. #10
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Help with formula for unique values - VBA

    Quote Originally Posted by rkohman View Post
    Thanks Glenn, That seems to be much closer. I've added a -1 to the end to remove the column header... but it is still giving me 1 more than is in the column.

    I have 18 unique values, (not including the column header,) and with the -1 it is showing 19.

    Any ideas why?
    =SUMPRODUCT(1/COUNTIF(A:A,A:A&""))

    It's counting 1 instance of "".
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  11. #11
    Registered User
    Join Date
    07-28-2016
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    17

    Re: Help with formula for unique values - VBA

    Quote Originally Posted by Tony Valko View Post
    =SUMPRODUCT(1/COUNTIF(A:A,A:A&""))

    It's counting 1 instance of "".
    ok. Thanks Tony. I figured that might be it.

    Cheers,
    R

  12. #12
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,055

    Re: Help with formula for unique values - VBA

    Thanks for the fix. I forgot about that!!

  13. #13
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Help with formula for unique values - VBA

    You're welcome. Thanks for the feedback!

+ 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] Unique values IF formula
    By De_Giels in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-21-2014, 08:38 AM
  2. [SOLVED] Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique values.
    By grphillips in forum Excel Programming / VBA / Macros
    Replies: 36
    Last Post: 06-09-2014, 02:12 PM
  3. [SOLVED] Need a formula that adds a set of values that are associated with a unique set of values
    By trevorgoulet in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-17-2013, 12:58 AM
  4. Unique values formula
    By Nate Westcott in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 01-06-2012, 02:52 PM
  5. formula to add unique values
    By TexRex68 in forum Excel General
    Replies: 13
    Last Post: 01-07-2010, 03:31 PM
  6. Replies: 8
    Last Post: 12-30-2008, 12:06 PM
  7. [SOLVED] Sorting unique values and returning values from a formula
    By MarcusA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-01-2006, 01:40 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