+ Reply to Thread
Results 1 to 3 of 3

count duplicates in groups of 4

Hybrid View

  1. #1
    Registered User
    Join Date
    11-27-2011
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    9

    count duplicates in groups of 4

    Hi,
    I need some help with vba macro.
    I have a table where column B has series of duplicate values. I need to count those duplicates in groups of 4 (column H) and then assign a group index on column I.
    The index value will be 1 for most of the groups unless the group is duplicate.
    My initial trial was to number the duplicates inside the group, but after this I’m stuck.
    Thanks in advance for you help.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: count duplicates in groups of 4

    Hi

    Try this:

    Sub test2()
    Dim lr, i, a As Long, Count As Integer, j, iIndex As Integer
    Dim curWks As Worksheet
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    
    Set curWks = ActiveSheet
    
    With curWks
    lr = .Range("A50000").End(xlUp).Row
    
    Count = 1
    iIndex = 1
    
    For i = 2 To lr
     .Cells(i, 8) = Count
     .Cells(i, 9) = iIndex
     
     
     If .Cells(i, 2) = .Cells(i + 1, 2) Then
     'Increment count if the next row is the same
        Count = Count + 1
        Else
    'Otherwise
    'Reset iIndex.
    iIndex = 1
    
    'Search backwards for the last time
    'we saw the value on teh next row. If found, set iIndex
    'to this value +1
        For j = i To 2 Step -1
         If .Cells(j, 2) = .Cells(i + 1, 2) Then
          iIndex = .Cells(j, 9) + 1
          Exit For
          End If
          Next j
     
     'Reset Count
        Count = 1
        End If
     
    'If we incremented count, and it hits 5, we must be in the
    'next group of 4
     If Count = 5 Then
         Count = 1
         iIndex = iIndex + 1
         End If
        
     Next i
     
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
     End With
    
    End Sub
    Hope this helps.

    Best regards, Rob.

  3. #3
    Registered User
    Join Date
    11-27-2011
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: count duplicates in groups of 4

    Thank you very much Rob,
    Everything works fine,
    Best Regards
    Alex BOtea

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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