How to count different names in a vba column?

Asked

Viewed 1,649 times

0

Good people, I have a question in a work in vba in which, in a column, has four brands of cars: inserir a descrição da imagem aqui

And you’re supposed to put on cell C15 the brand you want to look for and on cell D15 you have to show the number of cars there are of that brand thank you

  • Ever tried using formula? Putting the mark on C15 and the formula =CONT.SE(B2:B14;C15) in D15. Where B2:B14 is the range with the marks.

1 answer

1

Good morning, can use the next function that counts the single values:

Public Function CountUnique(rng As Range) As Integer
        Dim dict As Dictionary
        Dim cell As Range
        Set dict = New Dictionary
        For Each cell In rng.Cells
             If Not dict.Exists(cell.Value) Then
                dict.Add cell.Value, 0
            End If
        Next
        CountUnique = dict.Count
    End Function
  • Very good to use Dictionary as it is faster than interacting with the Excel spreadsheet. Only one addendum: This is a function in which an example of using it is: CountUnique(B3:B12)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.