Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
Por Domingsp
Posts
#30141
Olá, gostaria de contar no intervalo de S1:V8 quantos nomes aparecem pelo menos 2 vezes. A seguir exibir os nomes em lista na col X.
Fiz esse codigo no qual ele conta e exibe em X o nome. Mas do jeito que está, como são muitos nomes, eu teria que fazer uma linha de código para cada nome: QCatia, QGeraldo, QCassia...
Procuro uma solução melhor na qual ele verifica quais nomes aparecem pelo menos 2x e lista na col X.
Anexei uma figura que mostra a matriz e a forma como quero exibir(não precisa ser em ordem alfabetica).
Código: Selecionar todos
Sub CARNAVALESCOS()
Sheets("Plan6").Activate
  QCatia = Application.CountIf(Range("S1:V8"), "Catia")
   If QCatia >= 2 Then Cells(i + 1, 24) = Catia: i = i + 1
End if
Imagem
Avatar do usuário
Por gfranco
Avatar
#30142
Bom dia.
Veja se o que fiz te ajuda.
Você não está autorizado a ver ou baixar esse anexo.
Por osvaldomp
#30148
Código: Selecionar todos
Sub ListaNomes()
 Dim n As Range
  [X:Y] = "": [X1] = "nome": [Y1] = "qtde"
  For Each n In Range("S1:V8")
   If Application.CountIf(Range("S1:V8"), n.Value) > 1 And Application.CountIf([X:X], n.Value) = 0 Then
    Cells(Rows.Count, "X").End(3)(2) = n.Value
   End If
  Next n
 Range("Y2:Y" & Cells(Rows.Count, "X").End(3).Row).Formula = "=COUNTIF(S$1:V$8,X2)"
End Sub
Por juniorieq61
Posts
#30183
Olá amigo, pelo que entendi, você quer uma lista com os nomes únicos que aparecem naquele range e ao lado a quantidade de vezes que ele se repete certo?

Segue o código aqui e a pasta de trabalho anexada.
Código: Selecionar todos
Public Sub Main()
    Dim ws          As Worksheet
    Dim lRow        As Long
    Dim lCol        As Integer
    Dim iRow        As Long
    Dim iCol        As Integer
    Dim oDic        As Object
    Dim NewItem     As String
    Dim arr         As Variant
    Dim Key         As String
    Dim wFunction   As WorksheetFunction
    
    Set wFunction = Application.WorksheetFunction
    Set oDic = CreateObject("Scripting.Dictionary")
    Set ws = Planilha1
    
    With ws
        lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        oDic.CompareMode = TextCompare
        
        For iRow = 1 To lRow
            For iCol = 1 To lCol
                NewItem = .Cells(iRow, iCol).Value2
                If Not oDic.Exists(NewItem) Then oDic.Add NewItem, NewItem
            Next iCol
        Next iRow
           
        If .Range("H3").Value = "" Or .Range("I3").Value2 = "" Then GoTo Continue

        .Range("H3", .Range("H3").End(xlDown)).ClearContents
        .Range("I3", .Range("I3").End(xlDown)).ClearContents
        
Continue:
        arr = oDic.Items
        
        For iRow = LBound(arr) To UBound(arr)
            .Cells(iRow + 3, "H").Value2 = arr(iRow)
            .Cells(iRow + 3, "I").Value2 = _
                    wFunction.CountIf(.Range("A1", .Cells(lRow, lCol)), arr(iRow))
        Next iRow
    End With
End Sub
Espero que seja isso, abraço!
Você não está autorizado a ver ou baixar esse anexo.
Por Domingsp
Posts
#30446
Olá, Junior. Eu não precisava saber a quantidade de vezes que se repete. Só queria a lista dos nomes que aparecem pelo menos 2 vezes. Mas tudo já foi resolvido. De qualquer forma muito obrigado pela atenção.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord