Página 1 de 1

ComboBox com valor não repetidos

Enviado: 07 Mar 2017 às 10:36
por Krebs
Eu não estou conseguindo fazer com que a segunda parte pegue o item uma única vez.

Se eu Mudar o Nome na ComBoxLider para Thiago, eu quero que a minha ComboBoxProjeto me mostre todos os projetos dele, mas sem repeti-los

A minha primeria ComboBox (ComboBoxLider) nunca terá itens repetidos, pois fiz uma lista especifica para ela, mas no caso da ComboBoxProjeto a informção vem de uma base com itens repetidos.

Tentei usar o Dictionary, mas não estou conseguindo.
Código: Selecionar todos
Private Sub UserForm_Initialize()
Dim linha As Integer, coluna As Integer
    TextBoxHoje.Text = Format(Now(), "DD/MM/YYYY")
    linha = 13
    coluna = 4
    Me.ComboBoxLider.Clear
    With Sheets("Listas")
        Do While Not IsEmpty(.Cells(linha, coluna))
            Me.ComboBoxLider.AddItem .Cells(linha, coluna).Value
            linha = linha + 1
        Loop
    End With
End Sub
Código: Selecionar todos
Private Sub ComboBoxLider_Change()

    Dim linha As Integer, colunaProjeto As Integer, colunaLider As Integer
    Dim oDictionary As Object
    Set oDictionary = CreateObject("Scripting.Dictionary")
    linha = 3
    colunaProjeto = 5
    colunaLider = 6
    Me.ComboBoxProjeto.Clear
    With Sheets("Base")
        Do While Not IsEmpty(.Cells(linha, colunaProjeto))
            
            If .Cells(linha, colunaLider).Value = ComboBoxLider.Value Then
                    'Do Nothing
                If oDictionary.exists(.Cells(linha, colunaProjeto).Value) Then
                    'Do Nothing
                
                Else
                    Me.ComboBoxProjeto.AddItem .Cells(linha, colunaProjeto).Value
                    oDictionary.Add .Cells(linha, colunaProjeto).Value
                    
                End If
            Else
            
            GoTo Quit
            
            End If
            
Quit:
            linha = linha + 1
        Loop
    End With

End Sub
Me desculpem se já existia algo similar, tentei achar, mas não consegui.

Essa dúvida também foi postada no fórum mdsn: https://social.msdn.microsoft.com/Forum ... orum=vbapt

obs: Meu conhecimento em VBA é bem limitado

Desde de já agradeço por qualquer ajuda

Re: ComboBox com valor não repetidos

Enviado: 07 Mar 2017 às 11:39
por alexandrevba
Bom dia!!

Seria isso?
Código: Selecionar todos
Private Sub UserForm_Initialize()
Dim linha As Integer, coluna As Integer
Dim Rng As Range, Dn As Range
    TextBoxHoje.Text = Format(Now(), "DD/MM/YYYY")
    

Dim Dic As Object
    Set Rng = Range(Range("D14"), Range("D" & Rows.Count).End(xlUp))
    Set Dic = CreateObject("scripting.dictionary")
    Dic.CompareMode = vbTextCompare
    For Each Dn In Rng: Dic(Dn.Value) = Empty: Next
    With ComboBoxLider
        .RowSource = ""
        .List = Application.Transpose(Dic.keys)
    End With

    
End Sub
Att

ComboBox com valor não repetidos

Enviado: 07 Mar 2017 às 14:18
por Krebs
Seria para a segunda parte do código.
Código: Selecionar todos
Private Sub ComboBoxLider_Change()

    Dim linha As Integer, colunaProjeto As Integer, colunaLider As Integer
    Dim oDictionary As Object
    Set oDictionary = CreateObject("Scripting.Dictionary")
    linha = 3
    colunaProjeto = 5
    colunaLider = 6
    Me.ComboBoxProjeto.Clear
    With Sheets("Base")
        Do While Not IsEmpty(.Cells(linha, colunaProjeto))
            
            If .Cells(linha, colunaLider).Value = ComboBoxLider.Value Then
                    'Do Nothing
                If oDictionary.exists(.Cells(linha, colunaProjeto).Value) Then
                    'Do Nothing
                
                Else
                    Me.ComboBoxProjeto.AddItem .Cells(linha, colunaProjeto).Value
                    oDictionary.Add .Cells(linha, colunaProjeto).Value
                    
                End If
            Else
            
            GoTo Quit
            
            End If
            
Quit:
            linha = linha + 1
        Loop
    End With

End Sub

ComboBox com valor não repetidos

Enviado: 07 Mar 2017 às 16:13
por Krebs
Consegui obter a resposta em outro fórum

Segue resposta do Natan:

método Add do Dictionary necessita de dois parâmetros, um índice e um valor por isso o erro.

Altere para:
Código: Selecionar todos
oDictionary.Add .Cells(linha, colunaProjeto).Value , 1
Agora uma dica que eu já utilizei para não repetir itens no combobox...

considerando que todo o seu código estava inicialmente correto:
Código: Selecionar todos
Private Sub ComboBoxLider_Change()

    Dim linha As Integer, colunaProjeto As Integer, colunaLider As Integer
    linha = 3
    colunaProjeto = 5
    colunaLider = 6
    Me.ComboBoxProjeto.Clear
    With Sheets("Base")
        Do While Not IsEmpty(.Cells(linha, colunaProjeto))
            If .Cells(linha, colunaLider).Value = ComboBoxLider.Value Then
                Me.ComboboxProjeto.Value = .Cells(linha, colunaProjeto).Value 
                if Not Me.ComboboxProjeto.MatchFound then
                     Me.ComboBoxProjeto.AddItem .Cells(linha, colunaProjeto).Value
                end if
            End If
            linha = linha + 1
        Loop
    End With

End Sub 
Abraço!