Creer tableau à partir d'un comboBox VBA

hazein

XLDnaute Nouveau
Bonsoir,
Suite à un blocage, je vous fais appel. J'espère que vous pourrez m'aider.
Grâce à la communauté j'ai pu mettre en place une combobox récupérant les différentes catégorie (voir feuill1 et 3). J'utilise cette combobox pour créer un tableau par catégorie ( fruit et légume - voir feuille 2) Mais maintenant je souhaite, toujours à l'aide de ce combobox, creer un tableau fusion c'est à dire fruit et légume dans un même et un seul tableau (comme indiqué dans la feuille3). je veux trier la colonne types par ordre alphabétique
Pour infos les données sont présentes dans la feuille 1. Une dernière question, est il possible avec la macro actuelle (ComboBox1_GotFocus()) d'alimenter un champ supplémentaire dans la combobox ?Si oui comment? parce que j'ai essayé avec Me.ComboBox1.List ="All" mais cela ne fonctionne pas.

Merci d'avance
 

Pièces jointes

  • Hazein_v01.xls
    52.5 KB · Affichages: 88
  • Hazein_v01.xls
    52.5 KB · Affichages: 94
  • Hazein_v01.xls
    52.5 KB · Affichages: 98

hazein

XLDnaute Nouveau
Re : Creer tableau à partir d'un comboBox VBA

Salut Bebere,
j'ai essayé d'appliquer ta macro dans un cas concret cela fonctionne très bien pour les sélections unitaire mais par contre pour "tous" cela ne fonctionne pas.
Voici le code modifié:
Code:
Sub ok()

    tbl = Range("Feuil1!A2:C8")
    Range("A10:C100").ClearContents
    l = 10

    Select Case Feuil1.ComboBox1
    Case Feuil1.ComboBox1.Value 'la modif
        choix = Feuil1.ComboBox1.Value

    For i = 1 To UBound(tbl, 1)
        If tbl(i, 1) = choix Then
            Range("A" & l) = tbl(i, 1)
            Range("B" & l) = tbl(i, 2)
            Range("C" & l) = tbl(i, 3)
            l = l + 1
        End If
    Next i

'    Case Feuil1.ComboBox1.Value
'        choix = Feuil1.ComboBox1.Value
'
'    For i = 1 To UBound(tbl, 1)
'        If tbl(i, 1) = choix Then
'            Range("A" & l) = tbl(i, 1)
'            Range("B" & l) = tbl(i, 2)
'            Range("C" & l) = tbl(i, 3)
'            l = l + 1
'        End If
'    Next i

    Case "tout"
        Range("A10").Resize(UBound(tbl, 1), UBound(tbl, 1)) = tbl

    Case Else
        Exit Sub

    End Select
    
    derl = Range("A65536").End(xlUp).Row
    
    For i = 10 To derl
        For j = i + 1 To derl
            If Range("A" & i) = Range("A" & j) And Range("B" & i) = Range("B" & j) Then
                Range("C" & i) = Range("c" & i) + Range("C" & j)
                Range("C" & j) = ""
            End If
        Next j
    Next i

    For l = derl To 10 Step -1
        If Range("C" & l) = "" Then
            Range("A" & l & ":C" & l).Delete
        End If
    Next l
    Range("A9:C" & derl).Sort Key1:=Range("A10"), Order1:=xlAscending, Key2:=Range _
        ("B10"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
        :=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
        DataOption2:=xlSortNormal
        
    Feuil1.ComboBox1.Value = ""
    
End Sub

en fait je veux qu'il recupere la valeur du combobox automatiquement sans le mettre en dur. Ce code fonctionne bien mais pas pour "tous"(il n'affiche rien). Une idée?
 

hazein

XLDnaute Nouveau
Re : Creer tableau à partir d'un comboBox VBA

salut ,
je tiens à t'informer que dans la dernière colonne (dans le vrai fichier) je n'ai pas de valeur mais du texte.
J'ai toujours ce problème d'affichage avec "tous".Si tu pouvais m'aider ce serait super sympas de ta part.

Merci d'avance
 

hazein

XLDnaute Nouveau
Re : Creer tableau à partir d'un comboBox VBA

c'est bon j'ai reussi à faire fonctionner ta macro qui fonctionne très bien mais malheureusement j'ai trop de données (environs 6000 lignes) du coup ça met trop et ça fait planter le fichier excel.
Sinon j'ai trouvé une macro intéressante mais je n'arrive pas à la variabilisé:
Code:
Sub SansDoublons()
   [Feuill1!A1:C1000].AdvancedFilter Action:=xlFilterCopy, CopyToRange:=[Feuill1!G1], Unique:=True
End Sub

cette macro fonctionne très bien (recupere sans les doublons) et un autre soucis je n'arrive pas à copier la plage sur une autre feuille pourtant je fais bien [Feuil2!G1] mais sans résultat.
 

hazein

XLDnaute Nouveau
Re : Creer tableau à partir d'un comboBox VBA

merci beaucoup mais dans la 3 eme colonne je veux qu'il affiche la valeur correspondante. De plus le code ne fonctionne plus si je récupère la valeur du combobox (Feuil1.combobox1.value)

Code:
Sub ok()
    Dim Tbl, i As Long, L As Long, DerL As Long
    Dim MonDico As Object, Item

    Application.ScreenUpdating = False
    With Worksheets("Feuil1")
        .Unprotect
        .Range("A1").Sort Key1:=.Columns("A"), Header:=xlGuess
        DerL = .Range("A65536").End(xlUp).Row
        Tbl = .Range("A2:C" & DerL)
        .Protect
    End With
    'efface
    Range("A10:C" & Range("A65536").End(xlUp).Row + 1).ClearContents
    L = 10
    Set MonDico = CreateObject("scripting.dictionary")
    'catégorie,type sans doublon

    Select Case Feuil1.ComboBox1
    Case Feuil1.ComboBox1.Value
        'catégorie,type sans doublon
        For i = 1 To UBound(Tbl, 1)
            If Tbl(i, 1) = "fruit" And Not MonDico.Exists(Tbl(i, 1) & "-" & Tbl(i, 2)) Then MonDico.Add Tbl(i, 1) & "-" & Tbl(i, 2), Tbl(i, 1) & "-" & Tbl(i, 2)
        Next i

        For Each Item In MonDico.items
                Range("A" & L) = Left(Item, InStr(Item, "-") - 1)
                Range("B" & L) = Mid(Item, InStr(Item, "-") + 1)
            For i = 1 To UBound(Tbl, 1)
                If Tbl(i, 1) & "-" & Tbl(i, 2) = Item Then
                    Range("C" & L) = Range("C" & L) & Tbl(i, 3) & ","
                End If
            Next i
            Range("C" & L) = Left(Range("C" & L), Len(Range("C" & L)) - 1)
             L = L + 1
        Next Item

    'Case "legume"
     '   'catégorie,type sans doublon
     '   For i = 1 To UBound(Tbl, 1)
     '       If Tbl(i, 1) = "legume" And Not MonDico.Exists(Tbl(i, 1) & "-" & Tbl(i, 2)) Then MonDico.Add Tbl(i, 1) & "-" & Tbl(i, 2), Tbl(i, 1) & "-" & Tbl(i, 2)
     '   Next i

      '  For Each Item In MonDico.items
        '        Range("A" & L) = Left(Item, InStr(Item, "-") - 1)
      '          Range("B" & L) = Mid(Item, InStr(Item, "-") + 1)
           ' For i = 1 To UBound(Tbl, 1)
          '      If Tbl(i, 1) & "-" & Tbl(i, 2) = Item Then
           '         Range("C" & L) = Range("C" & L) & Tbl(i, 3) & ","
        '        End If
         '   Next i
       '     Range("C" & L) = Left(Range("C" & L), Len(Range("C" & L)) - 1)
          '   L = L + 1
      '  Next Item
        
    Case "tout"
        'catégorie,type sans doublon
        For i = 1 To UBound(Tbl, 1)
            If Not MonDico.Exists(Tbl(i, 1) & "-" & Tbl(i, 2)) Then MonDico.Add Tbl(i, 1) & "-" & Tbl(i, 2), Tbl(i, 1) & "-" & Tbl(i, 2)
        Next i

        For Each Item In MonDico.items
                Range("A" & L) = Left(Item, InStr(Item, "-") - 1)
                Range("B" & L) = Mid(Item, InStr(Item, "-") + 1)
            For i = 1 To UBound(Tbl, 1)
                If Tbl(i, 1) & "-" & Tbl(i, 2) = Item Then
                    Range("C" & L) = Range("C" & L) & Tbl(i, 3) & ","
                End If
            Next i
            Range("C" & L) = Left(Range("C" & L), Len(Range("C" & L)) - 1)
             L = L + 1
        Next Item

    Case Else
        Exit Sub

    End Select

        Range("A9:C" & DerL).Sort Key1:=Range("A10"), Order1:=xlAscending, Key2:=Range("B10"), Order2:=xlAscending, _
             Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
                                       DataOption2:=xlSortNormal

    Feuil1.ComboBox1.Value = ""

End Sub
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 296
Messages
2 086 967
Membres
103 411
dernier inscrit
jamjam6767