Notifications
Clear all

COMBOBOX - Remover duplicatas e ordenar

2 Posts
2 Usuários
0 Reactions
1,037 Visualizações
(@romelvsr)
Posts: 39
Eminent Member
Topic starter
 

Amigos, boa tarde!
Estou começando no VBA e não consigo unir o código de "remoção de duplicatas" ao de "ordenar". Alguém pode me ajudar, acredito que seja simples mas não estou conseguindo. Segue link para baixar a planilha: https://www.dropbox.com/s/d9vtd1lhnf3qt ... .xlsm?dl=0

Private Sub ComboBox1_Enter()

'/////////////REMOÇÃO DE DUPLICATAS\\\\\\\\\\\\
linha = 2
Do While Plan1.Cells(linha, 1) <> ""
linha = linha + 1
Loop
Fim = linha - 1

With Plan1

For linha = 2 To Fim
'A linha abaixo verifica se tem repetição de dados, para não duplicar ao carregar o Combobox
Registro = WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(linha, 1)), Cells(linha, 1))
If Registro = 1 Then

ComboBox1.AddItem .Cells(linha, 1)

End If
Next linha
End With

'//////NÃO ESTOU CONSEGUINDO UNIR O CÓDIGO ACIMA A ESSE\\\\\\
'------------------Código para ordenar------------------------------------

'Dim Ray, i As Integer, j As Integer, temp As String
'With ComboBox1
'Ray = Application.Transpose(.List)
'For i = 1 To UBound(Ray) - 1
'For j = i To UBound(Ray)
'If Ray(j) < Ray(i) Then
'temp = Ray(i)
'Ray(i) = Ray(j)
'Ray(j) = temp
'End If
'Next j
'Next i
'.List = Ray
'End With

End Sub

 
Postado : 21/04/2018 9:54 am
(@klarc28)
Posts: 971
Prominent Member
 

Cinco dicas que foram muito úteis para mim:

1) Quando não sei fazer algo no VBA, vou ao menu EXIBIÇÃO >> MACROS >> GRAVAR MACRO
Faço o que eu quero aí volto ao menu EXIBIÇÃO >> MACROS >> PARAR GRAVAÇÃO
Aperto Alt + F11 e vejo como a macro fez aquilo. Tento entender e tento adaptar.

2) Quando vou criar um código e o resultado não está saindo como o esperado, entro no código e vou apertando F8 para executar passo a passo, aí vou passando o mouse sobre as variáveis para verificar se o valores delas estão corretos, já consertei milhares de códigos dessa forma.

3) Antes de executar o código, vou ao menu Depurar >> Compilar. Isso ajuda a corrigir erros mais simples, como o nome de uma variável digitado errado.

4) Declaro todas as variáveis. Isso também evita erros.

5) Sempre uso o Option Explicit lá no início. Ele me obriga a declarar as variáveis.

Private Sub ComboBox1_Enter()
    

    
End Sub


Private Sub UserForm_Initialize()
    linha = 2
    Do While Plan1.Cells(linha, 1) <> ""
        linha = linha + 1
    Loop
    Fim = linha - 1
    
    With Plan1
        
        For linha = 2 To Fim
            'A linha abaixo verifica se tem repetição de dados, para não duplicar ao carregar o Combobox
            Registro = WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(linha, 1)), Cells(linha, 1))
            If Registro = 1 Then
                
                ComboBox1.AddItem .Cells(linha, 1)
                
            End If
        Next linha
    End With
    
    
    '//////NÃO ESTOU CONSEGUINDO UNIR O CÓDIGO ACIMA A ESSE\\\\\\
    
    'Dim Ray, i As Integer, j As Integer, temp As String
    'With ComboBox1
    'Ray = Application.Transpose(.List)
    'For i = 1 To UBound(Ray) - 1
    'For j = i To UBound(Ray)
    'If Ray(j) < Ray(i) Then
    'temp = Ray(i)
    'Ray(i) = Ray(j)
    'Ray(j) = temp
    'End If
    'Next j
    'Next i
    '.List = Ray
    'End With
    
    
    Dim I As Long
    Dim J As Long
    Dim ANTERIOR As String
    Dim POSTERIOR As String
    
    If ComboBox1.ListCount >= 1 Then
        For I = 0 To ComboBox1.ListCount - 2
            For J = I + 1 To ComboBox1.ListCount - 1
                
                ANTERIOR = ComboBox1.List(I)
                POSTERIOR = ComboBox1.List(J)
                
                If ANTERIOR > POSTERIOR Then
                    
                    ANTERIOR = ComboBox1.List(J)
                    POSTERIOR = ComboBox1.List(I)
                    
                    ComboBox1.List(I) = ANTERIOR
                    ComboBox1.List(J) = POSTERIOR
                End If
                
            Next J
            
        Next I
        
    End If
End Sub
 
Postado : 21/04/2018 11:25 am