Excel VBA: How to Count Duplicate Value

polars_k 21 Reputation points
2020-12-24T14:46:44.507+00:00

Column A have many ID, there are duplicate value such as cell(A2) ID 300502489400, cell(A3) ID 300502520900, cell(A4) ID 300502520900, cell(A5) ID 300502520900, cell(A6) ID 300502523900, cell(A7) ID 300502520900,

i need to count number of duplicate and report in Column B result such as cell(B2) =1, cell(B3) =4, cell(B4) =4, cell(B5) =4, cell(B6) =1, cell(B7) =1,
51106-capture.jpg

Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,863 questions
{count} votes

Accepted answer
  1. HansV 966 Reputation points MVP
    2020-12-24T16:59:27.05+00:00

    You don't need VBA for that In B2:

    =COUNTIF($A$2:$A$22,A22)

    Fill down.

    If you really want to use VBA:

    Sub Test()
        With Range("B2:B22")
            .Formula = "=COUNTIF($A$2:$A$22,A2)"
            .Value = .Value
        End With
    End Sub
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. HansV 966 Reputation points MVP
    2020-12-24T20:52:24.343+00:00

    Try this:

    Sub count_duplicate_value3()
        Dim m As Long
        m = Range("A" & Rows.Count).End(xlUp).Row
        With Range("B2:B" & m)
            .Formula = "=COUNTIF($A$2:$A$" & m & ",A2)"
            .Value = .Value
        End With
    End Sub
    

    If you don't want a value in the last used row, change the line that sets m to

        m = Range("A" & Rows.Count).End(xlUp).Row - 1
    
    1 person found this answer helpful.
    0 comments No comments

  2. polars_k 21 Reputation points
    2020-12-25T01:17:15.21+00:00

    HansV-7609 Great ! It'work. Thank you so much.
    51154-5.jpg

    0 comments No comments

  3. Emi Zhang-MSFT 29,996 Reputation points Microsoft External Staff
    2020-12-25T06:30:33.137+00:00

    Hi @polars_k ,
    Thanks for "Accept Answer" answer of HansV, I also suggest you upvote it. Other partners who read the forums with the same issue can get more information from the correct result.

    Thank you.
    :)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.