1

Column A include a list of numbers, some are duplicated

Column B include a list of numbers from 1-12 that represent the months

I need to count the unique numbers in column A where the month in column B = 1

Example:

ColumnA ColumnB
123     1
223     2
312     3
412     1
123     1
312     2
123     3

So here for example 123 show up twice when B=1 and 412 once on the same month so the unique count should be = 2

Kenneth L
  • 14,104
Chuku
  • 11

1 Answers1

0

I know you want a solution using countif, but I don't know if it can be done with only countif; So, as an alternative, this VBa does it

Option Explicit
Sub CountDuplicatesPerMonth()

Dim row As Integer
row = 1

Range("C:C").Value = "" 'clear the results


Do While (Range("A" & row).Value <> "")

    Dim val As String
    val = Range("A" & row).Value

    Dim month As Integer
    month = Range("B" & row).Value

    Range("C" & row).Value = 0

    Dim innerRow As Integer
    innerRow = 1

    Do While (Range("A" & innerRow).Value <> "")

        Dim innerVal As String
        innerVal = Range("A" & innerRow).Value

        Dim innerMonth As Integer
        innerMonth = Range("B" & innerRow).Value

        If (innerVal = val And innerMonth = month) Then
            Range("C" & row).Value = Range("C" & row).Value + 1
        End If

        innerRow = innerRow + 1

    Loop

   row = row + 1

Loop

End Sub

Before
enter image description here

After VBa runs

enter image description here

Dave
  • 25,513