+ Reply to Thread
Results 1 to 19 of 19

Return Unique Values with count

Hybrid View

  1. #1
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Return Unique Values with count

    I'm using a macro that works fine to return a unique list from column C based on column D having the word "Major Task". Right now, I also have a formula applied to column H giving the count of "Sub Tasks" under each one of the "Major Tasks".

    How can I instead get the result found in column I? Basically the count just to the right of the "Major Task".

    Can this be done within the FilterUnique macro or is there another formula I can use? This is done for feeding the DV in M10 and M12 which is used on a larger scale for charting.
    Attached Files Attached Files
    HTH
    Regards, Jeff

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    As a starter...
    Option Explicit
    
    Sub CountTask()
    Dim cnt As Long, SubCnt As Long, i As Long, lr As Long
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    Dim fnd1 As Range, fnd2 As Range
    cnt = Application.WorksheetFunction.CountIf(Columns(3), "Major*")
    For i = 1 To cnt
        Set fnd1 = Range("C:C").Find("Major Task " & i, LookIn:=xlValues, lookat:=xlWhole)
        Set fnd2 = Range("C:C").Find("Major Task " & i + 1, LookIn:=xlValues, lookat:=xlWhole)
        If i = cnt Then
            SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row, 3), Cells(lr, 3)), "Sub*")
        Else
            SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row, 3), Cells(fnd2.Row, 3)), "Sub*")
        End If
        Range("G" & i + 7).Resize(, 2) = Array("Major Task " & i, SubCnt)
    Next i
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    I do believe that is going to work just fine. Thank you very much.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    .........................................
    Thanks.gif

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    Hi Sintek,

    What if the Major Tasks were not ordinal as in Major Task 1,2,3, etc, but instead, they are like...

    C8 = Produces classified letters
    C16 = Maintains database servers
    C22 = Delivers classified letters

    ...and so with this structure, can I get the count between C8 and C16, C16 and C22, and then what's left?
    Last edited by jeffreybrown; 04-06-2019 at 12:46 PM.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    Okay, not sure if this is a good way to go about it, but I modified to this. The Sub Tasks can also be actually task that do not start with Sub.

    Sub FilterUnique()
        Dim rng As Range, Dn As Range
        With Sheet22
            .Columns(7).Clear
            Set rng = .Range(.Range("C8"), .Range("C" & .Rows.Count).End(xlUp))
        End With
        Debug.Print rng.Address
        With CreateObject("scripting.dictionary")
            .CompareMode = vbTextCompare
            For Each Dn In rng
                If Not .Exists(Dn.Value) And Dn.Offset(, 1).Value = "Major Task" Then
                    .Add Dn.Value, ""
                End If
            Next
            Sheet22.Range("G8").Resize(.Count).Value = Application.Transpose(.keys)
        End With
        Call CountTask
    End Sub
    Sub CountTask()
        Dim cnt As Long, SubCnt As Long, i As Long, lr As Long
        lr = Cells(Rows.Count, "A").End(xlUp).Row
        Dim fnd1 As Range, fnd2 As Range
        cnt = Application.WorksheetFunction.CountIf(Columns(4), "Major*")
        For i = 1 To cnt
            Set fnd1 = Range("C:C").Find(Range("G" & i + 7), LookIn:=xlValues, lookat:=xlWhole)
            Set fnd2 = Range("C:C").Find(Range("G" & i + 8), LookIn:=xlValues, lookat:=xlWhole)
            If i = cnt Then
                SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row + 1, 3), Cells(lr, 3)), "*")
            Else
                SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row + 1, 3), Cells(fnd2.Row - 1, 3)), "*")
            End If
            Range("H" & i + 7).Value = SubCnt
        Next i
    End Sub

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    apologies...had to put the little one down
    Last edited by sintek; 04-06-2019 at 01:25 PM.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    No worries, I just had to run out to pick up my grandson.

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    So are the Headers now gonna be "Major Task*" and the sub headers "Sub*" or something else...

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    What about....
    Sub CountTask()
    Dim cnt As Long, SubCnt As Long, i As Long, lr As Long
    Dim rng As Range, fnd1 As Range, fnd2 As Range
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    cnt = Application.WorksheetFunction.CountIf(Columns(4), "Major*")
    Set rng = Range("D8:D" & lr)
    With rng
        .AutoFilter 1, "Major*"
        .Offset(, -1).SpecialCells(12).Copy Range("G8")
        .AutoFilter 1
    End With
    For i = 1 To cnt
        Set fnd1 = Range("C:C").Find(Range("G" & i + 7), LookIn:=xlValues, lookat:=xlWhole)
        Set fnd2 = Range("C:C").Find(Range("G" & i + 8), LookIn:=xlValues, lookat:=xlWhole)
        If i = cnt Then
            SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row + 1, 3), Cells(lr, 3)), "*")
        Else
            SubCnt = Application.WorksheetFunction.CountIf(Range(Cells(fnd1.Row + 1, 3), Cells(fnd2.Row - 1, 3)), "*")
        End If
        Range("H" & i + 7).Value = SubCnt
    Next i
    End Sub
    Last edited by sintek; 04-06-2019 at 01:40 PM.

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    Sorry about that. Here is an updated file which I should have posted the first time. Rookie mistake.
    Attached Files Attached Files

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    K...See amended in Post 10

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    Hi Sintek,

    With a couple of tweaks, this works. Thanks again.

    Sub CountTask2()
        Dim cnt As Long, SubCnt As Long, i As Long, lr As Long
        Dim rng As Range, fnd1 As Range, fnd2 As Range, rngMyRange As Range
        With Sheet22
            lr = .Cells(.Rows.Count, "A").End(xlUp).Row
            .Columns("G:H").ClearContents
            cnt = Application.WorksheetFunction.CountIf(.Columns(4), "Major*")
            Set rng = .Range("D8:D" & lr)
            With rng
                .AutoFilter 1, "Major*"
            End With
            rng.Offset(, -1).SpecialCells(12).Copy
            .Range("G8").PasteSpecial xlPasteValues
            rng.AutoFilter
            For i = 1 To cnt
                Set fnd1 = .Range("C:C").Find(.Range("G" & i + 7), LookIn:=xlValues, lookat:=xlWhole)
                Set fnd2 = .Range("C:C").Find(.Range("G" & i + 8), LookIn:=xlValues, lookat:=xlWhole)
                If i = cnt Then
                    SubCnt = Application.WorksheetFunction.CountIf(.Range(.Cells(fnd1.Row + 1, 3), .Cells(lr, 3)), "*")
                Else
                    SubCnt = Application.WorksheetFunction.CountIf(.Range(.Cells(fnd1.Row + 1, 3), .Cells(fnd2.Row - 1, 3)), "*")
                End If
                .Range("H" & i + 7).Value = SubCnt
            Next i
            .Columns(7).EntireColumn.AutoFit
            Set rngMyRange = .Range("G8:G" & cnt + 7)
            ActiveWorkbook.Names.Add Name:="rngDV", RefersTo:=rngMyRange
        End With
    End Sub

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,608

    Re: Return Unique Values with count

    Sub test()
        Dim a, b, i As Long, n As Long
        a = Range("c8", Range("c" & Rows.Count).End(xlUp)).Value
        ReDim b(1 To UBound(a, 1), 1 To 2)
        For i = 1 To UBound(a, 1)
            If a(i, 1) Like "Major Task*" Then
                n = n + 1: b(n, 1) = a(i, 1)
            Else
                If n > 0 Then b(n, 2) = b(n, 2) + 1
            End If
        Next
        With Range("g8:h8")
            .Resize(Cells.SpecialCells(11).Row - 7).ClearContents
            .Resize(n).Value = b
        End With
    End Sub

  15. #15
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    Hi Jindon and thank you for this solution.

    Column D is the column with "Major Task" and so the title for each "Major Task" I would like to return is from column C.

    a = Range("D8", Range("D" & Rows.Count).End(xlUp)).Value
    Easy enough to change the range to reference where to find "Major Task", but how to have the code offset that range by -1 columns?

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,608

    Re: Return Unique Values with count

    Ahh, I see...
    Sub test()
        Dim a, b, i As Long, n As Long
        a = Range("c8", Range("c" & Rows.Count).End(xlUp)).Resize(, 2).Value 'refers to C:D
        ReDim b(1 To UBound(a, 1), 1 To 2)
        For i = 1 To UBound(a, 1)
            If a(i, 2) Like "Major Task*" Then 'colD
                n = n + 1: b(n, 1) = a(i, 1) ' item in colC
            Else
                If n > 0 Then b(n, 2) = b(n, 2) + 1
            End If
        Next
        With Range("g8:h8")
            .Resize(Cells.SpecialCells(11).Row - 7).ClearContents
            .Resize(n).Value = b
        End With
    End Sub

  17. #17
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Return Unique Values with count

    That does it. Thank you Jindon.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,608

    Re: Return Unique Values with count

    You are welcome and thanks for the rep.

  19. #19
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Return Unique Values with count

    Great stuff jindon...Tx for the awesome code...love learning from you...

+ 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. Create a unique list with a count against the unique values
    By barber87 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-12-2017, 10:13 AM
  2. [SOLVED] Using INDEX MATCH to return unique values for non-unique search term
    By rico_suave in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 06-03-2015, 01:53 AM
  3. [SOLVED] Count then Delete Duplicate Values and put count next to now unique value
    By flipjarg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-12-2014, 04:22 PM
  4. [SOLVED] Unique Total Value Count per Unique Lookup Values
    By KnightVision in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-02-2014, 05:03 AM
  5. [SOLVED] count unique values based on unique values
    By neetu.aggarwal in forum Excel General
    Replies: 13
    Last Post: 10-23-2012, 04:00 AM
  6. [SOLVED] How To Count Unique Values in COL A Subject for each unique value in COL B ??
    By amirtehrani in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-06-2012, 03:00 AM
  7. Replies: 17
    Last Post: 08-24-2009, 08:58 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