+ Reply to Thread
Results 1 to 13 of 13

VBA to show the count of unique records

Hybrid View

muralidaran VBA to show the count of... 04-29-2014, 11:57 AM
hemesh Re: VBA to show the count of... 04-29-2014, 01:30 PM
sktneer Re: VBA to show the count of... 04-29-2014, 01:42 PM
hemesh Re: VBA to show the count of... 04-30-2014, 04:15 AM
sktneer Re: VBA to show the count of... 04-29-2014, 01:41 PM
Leith Ross Re: VBA to show the count of... 04-29-2014, 02:42 PM
Winon Re: VBA to show the count of... 04-29-2014, 03:18 PM
Leith Ross Re: VBA to show the count of... 04-29-2014, 03:26 PM
muralidaran Re: VBA to show the count of... 04-29-2014, 05:32 PM
Trebor76 Re: VBA to show the count of... 04-29-2014, 08:03 PM
Leith Ross Re: VBA to show the count of... 04-29-2014, 08:56 PM
muralidaran Re: VBA to show the count of... 04-30-2014, 03:46 PM
Leith Ross Re: VBA to show the count of... 04-30-2014, 04:03 PM
  1. #1
    Forum Contributor
    Join Date
    01-08-2012
    Location
    Saudi Arabia, Dammam
    MS-Off Ver
    MS Office LTSC Professional Plus 2021
    Posts
    113

    VBA to show the count of unique records

    Hi
    I have a excel file were i'm using more than 25000 cells and most of the records in it will be duplicate records. In order to get the unique records im using the "Remove duplicate option" but were as to make my summary report im using a formula to count the unique records thus increasing my file size gradually since im going to use it for more records.

    So i wanted to check the possibility to do this using vba to preserve my file size.

    It may not be very clear with my explanation, so i have attached the sample file.

    Thanks
    Attached Files Attached Files
    Last edited by muralidaran; 04-30-2014 at 03:46 PM.
    Thanks & Regards
    Muralidaran.

  2. #2
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: VBA to show the count of unique records

    if you want to count the unique records only then single cell solution is
    copy paste below in C2 then hold control and shift then hit enter to make it array formula
    =SUM(IF(FREQUENCY(MATCH(A2:A75,A2:A75,0),MATCH(A2:A75,A2:A75,0))>=1,1,0))

    change the references as per of your actual data

    if you want to have unique records preserving your actual data then select your data with headers then go to data tab---> Sort and Filter -----> Select Advance.
    select copy to another location (enter the cell where you want your unique data to start) hit Ok.
    Last edited by hemesh; 04-29-2014 at 01:38 PM.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WANT TO SAY THANKS, HIT ADD REPUTATION (*) AT THE BOTTOM LEFT CORNER OF THE POST

    More we learn about excel, more it shows us, how less we know about it.

    for chemistry
    https://www.youtube.com/c/chemistrybyshivaansh

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA to show the count of unique records

    @hemesh

    Good one hemesh.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  4. #4
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: VBA to show the count of unique records

    Quote Originally Posted by sktneer View Post
    @hemesh

    Good one hemesh.
    Thanks Sktneer !

  5. #5
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: VBA to show the count of unique records

    Moreover if you want to do it through VBA code, try this code......
    Sub countunique()
    Dim rng, chkrng, cell As Range
    Dim lr As Long, n, count As Integer
    lr = Cells(Rows.count, 1).End(xlUp).Row
    Set rng = Range("A2:A" & lr)
    For Each cell In rng
        Set chkrng = Range("A2:A" & cell.Row)
        n = WorksheetFunction.CountIf(chkrng, cell)
        If n = 1 Then
            count = count + 1
        End If
    Next cell
    MsgBox "Unique Records in the column A are " & count
    End Sub

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to show the count of unique records

    Hello muralidaran,

    Here is the fastest method to use in VBA for finding unique values. On my computer it would take about 1.3 seconds to scan 25000 rows and return the unique records.

    The attached workbook has the macro below added to it and a button on the worksheet to run it. All previous values in the "Uniques" column are cleared each time the macro is run.

    Sub GetUniqueValues()
    
        Dim DstRng  As Range
        Dim Dict    As Object
        Dim EndRow  As Long
        Dim Key     As Variant
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Sheet1
            
            Set DstRng = Wks.Range("D2")
                EndRow = Wks.Cells(Rows.Count, DstRng.Column).End(xlUp).Row
                If EndRow >= DstRng.Row Then DstRng.Resize(EndRow - DstRng.Row + 1, 1).ClearContents
            
            Set Rng = Wks.Range("A2")
                EndRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                If EndRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
                
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Key In Rng.Cells
                    Key = Trim(Key)
                    If Key <> "" Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, 1
                        End If
                    End If
                Next Key
                
            If Dict.Count > 1 Then
                DstRng.Resize(Dict.Count, 1).Value = Application.Transpose(Dict.Keys)
            Else
                DstRng.Value = Dict.Keys
            End If
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    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 to show the count of unique records

    @ Leith Ross,

    Hello Leith,

    What an excellent piece of Coding!

    Just to ensure D1 does not lose its heading, I would tweak your Code as follow.

    Option Explicit
    Sub GetUniqueValues()
    
        Dim DstRng  As Range
        Dim Dict    As Object
        Dim EndRow  As Long
        Dim Key     As Variant
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Sheet1
            Wks.Range("D1") = "Unique Values"
            
            Set DstRng = Wks.Range("D2")
                EndRow = Wks.Cells(Rows.Count, DstRng.Column).End(xlUp).Row
                If EndRow >= DstRng.Row Then DstRng.Resize(EndRow - DstRng.Row + 1, 1).ClearContents
            
            Set Rng = Wks.Range("A2")
                EndRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                If EndRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
                
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Key In Rng.Cells
                    Key = Trim(Key)
                    If Key <> "" Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, 1
                        End If
                    End If
                Next Key
                
            If Dict.Count > 1 Then
                DstRng.Resize(Dict.Count, 1).Value = Application.Transpose(Dict.Keys)
            Else
                DstRng.Value = Dict.Keys
            End If
        
    End Sub
    Kind 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] .

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to show the count of unique records

    Hello Winon,

    Thanks for the tweak. I have included it in the updated macro and workbook.
    Sub GetUniqueValues()
    
        Dim DstRng  As Range
        Dim Dict    As Object
        Dim EndRow  As Long
        Dim Key     As Variant
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = Sheet1
            
            Wks.Range("D1:E1") = Array("Uniques", "Count")
            
            Set DstRng = Wks.Range("D2:E2")
                EndRow = Wks.Cells(Rows.Count, DstRng.Column).End(xlUp).Row
                If EndRow >= DstRng.Row Then DstRng.Resize(EndRow - DstRng.Row + 1, 2).ClearContents
            
            Set Rng = Wks.Range("A2")
                EndRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                If EndRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
                
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Key In Rng.Cells
                    Key = Trim(Key)
                    If Key <> "" Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, 1
                        Else
                            Dict(Key) = Dict(Key) + 1
                        End If
                    End If
                Next Key
                
            Set DstRng = DstRng.Resize(Dict.Count, 2)
            
            If Dict.Count > 1 Then
                DstRng.Columns(1).Value = Application.Transpose(Dict.Keys)
                DstRng.Columns(2).Value = Application.Transpose(Dict.Items)
            Else
                DstRng.Columns(1).Value = Dict.Keys
                DstRng.Columns(2).Value = Dict.Items
            End If
        
    End Sub

  9. #9
    Forum Contributor
    Join Date
    01-08-2012
    Location
    Saudi Arabia, Dammam
    MS-Off Ver
    MS Office LTSC Professional Plus 2021
    Posts
    113

    Re: VBA to show the count of unique records

    Thanks everyone.....
    Mr. Leith Ross your code works fine but my requirement is to show the running count in column B, could you please help me to provide VBA code to show the running total in column B.

    Thanks.
    Last edited by muralidaran; 04-29-2014 at 05:43 PM.

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to show the count of unique records

    Hi muralidaran,

    I'll let Leith provide an answer on his fantastic code, but I'd thought I'd also post this as a possible alternative to simply return the count of the unique entries in a range:

    Option Explicit
    Sub Macro1()
    
        Dim lngStartRow As Long
        Dim strMyCol As String
        Dim lngLastRow As Long
        Dim dblUniqueCount As Double
        
        lngStartRow = 2 'Starting row number for the data. Change to suit.
        strMyCol = "B" 'Column letter containing the data. Change to suit.
        lngLastRow = Cells(Rows.Count, strMyCol).End(xlUp).Row
        
        On Error Resume Next
            dblUniqueCount = Evaluate("SUM(IF(FREQUENCY(MATCH(" & strMyCol & lngStartRow & ":" & strMyCol & lngLastRow & "," & strMyCol & lngStartRow & ":" & strMyCol & lngLastRow & ",0),MATCH(" & strMyCol & lngStartRow & ":" & strMyCol & lngLastRow & "," & strMyCol & lngStartRow & ":" & strMyCol & lngLastRow & ",0))>=1,1,0))")
        On Error GoTo 0
        
        MsgBox dblUniqueCount
    
    End Sub
    Regards,

    Robert
    Last edited by Trebor76; 04-29-2014 at 08:17 PM.
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to show the count of unique records

    Hello muralidaran,

    Here is the updated macro. This will provide you with a running total in column "B". It has been added to the attached workbook.

    Sub GetUniqueValues()
    
        Dim Cell    As Range
        Dim DstRng  As Range
        Dim Dict    As Object
        Dim EndRow  As Long
        Dim Key     As Variant
        Dim n       As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
        
            Set Wks = Sheet1
            
            Set DstRng = Wks.Range("D2:E2")
                EndRow = Wks.Cells(Rows.Count, DstRng.Column).End(xlUp).Row
                If EndRow >= DstRng.Row Then DstRng.Resize(EndRow - DstRng.Row + 1, 2).ClearContents
            
            Set Rng = Wks.Range("A2")
                EndRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
                If EndRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(EndRow - Rng.Row + 1, 1)
                
                Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
            Application.ScreenUpdating = False
            
                For Each Cell In Rng.Cells
                    Key = Trim(Cell)
                    If Key <> "" Then
                        If Not Dict.Exists(Key) Then
                            Dict.Add Key, 1
                            Cell.Offset(0, 1).Value = 1
                        Else
                            n = Dict(Key) + 1
                            Dict(Key) = n
                            Cell.Offset(0, 1).Value = n
                        End If
                    End If
                Next Cell
                
            Application.ScreenUpdating = True
            
    End Sub

  12. #12
    Forum Contributor
    Join Date
    01-08-2012
    Location
    Saudi Arabia, Dammam
    MS-Off Ver
    MS Office LTSC Professional Plus 2021
    Posts
    113

    Re: VBA to show the count of unique records

    Hi Leith Ross
    Thanks for the code, working as required.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to show the count of unique records

    Hello muralidaran,

    You're welcome. My apologies for not understanding what you wanted initially.

+ 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. Count Unique records
    By guido167 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-23-2013, 03:08 PM
  2. Calculating Unique records row count
    By plk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2012, 04:26 PM
  3. Excel 2007 : Count Unique Records
    By stevetothink in forum Excel General
    Replies: 2
    Last Post: 11-04-2011, 04:22 PM
  4. Count unique records with criteria
    By vancoservices in forum Excel General
    Replies: 4
    Last Post: 08-24-2010, 09:13 AM
  5. Advanced filter to show unique records
    By ResulG in forum Excel General
    Replies: 3
    Last Post: 12-04-2006, 09:06 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