0

I looked into the forum and I cannot really find the right answer to my problem. I have a list which looks like this:

ID ID2

SS_ID 1            T1              
SS_ID 1            T2
SS_ID 1            T2
SS_ID 1            T3
SS_ID 4            T1
SS_ID 4            T5

There is the 1st column which has IDs. The thing is that in the second column I have also ID numbers which are related with the 1st column.

So the list is in a sheet in Excel and I am trying to find with the help of VBA the number of Users from the 2nd column who are related to the 1st column. If by any chance there is a solution without VBA please feel free to help me. But as far as I can see it I only see the solution in VBA.

FYI I generate the list from a macro.I also found a way to remove the duplicates without deleting the rows which is necessarily in my case.

To Sum up, I need the sum of "unique" values from the 2nd column which are related to the 1st column. It is like a countif function but a little more complicated.

The result should look like this:

SS_ID 1    =    3
SS_ID 4    =    2

I am completely new to VBA.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

3 Answers3

3

No need for unless you absolutely require it.

=SUMPRODUCT((A$2:INDEX(A:A, MATCH("Z", A:A))=D2)/(COUNTIFS(B$2:INDEX(B:B, MATCH("ZZZ", A:A)), B$2:INDEX(B:B, MATCH("ZZZ", A:A)),  A$2:INDEX(A:A, MATCH("Z", A:A)), D2)+(A$2:INDEX(A:A, MATCH("Z", A:A))<>D2)))

enter image description here

1

Here is a macro-based solution:

Sub UniqueTable()
    Dim i As Long, N As Long, c As Collection, M As Long
    Dim j As Long, vd As String, va As String

    Columns("A:A").Copy Range("D1")

    ActiveSheet.Range("D:D").RemoveDuplicates Columns:=1, Header:=xlYes

    Range("E1").Value = "# of unique values"

    N = Cells(Rows.Count, "D").End(xlUp).Row
    M = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 2 To N
        Set c = Nothing
        Set c = New Collection
        vd = Cells(i, "D").Value
        For j = 2 To M
            va = Cells(j, "A").Value
            If va = vd Then
                On Error Resume Next
                    c.Add Cells(j, "B").Value, CStr(Cells(j, "B").Value)
                On Error GoTo 0
            End If
        Next j
        Cells(i, "E").Value = c.Count
    Next i
End Sub

enter image description here

You can easily modify the code to place the little results table anywhere you like. If you add/remove/modify items in cols A and B, just rerun the code.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
0

Solution without VBA

You can use something like that: =SUMIFS(C1:C6;A1:A6;"=SS_ID 4")

The excel funtion "SOMMA.PIU.SE" in the attached image is "SUMIFS" in english. The SUMIFS function adds all of its arguments (A1:A6) that meet multiple criteria (A1:A6 => "=SS_ID 4").

robotu
  • 203
  • 2
  • 7