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 Krebs
Posts
#20935
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
Editado pela última vez por Krebs em 07 Mar 2017 às 14:19, em um total de 1 vez.
#20938
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
Por Krebs
Posts
#20945
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
Por Krebs
Posts
#20959
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!
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